Python程式:列印字典中所有鍵值對的和
在Python中,字典是一種資料結構的實現,通常被稱為關聯陣列。字典由一組鍵值對組成。每個鍵值組合對應一個鍵及其對應的值。
在這篇文章中,我們將學習一個Python程式,用於列印字典中所有鍵值對的和。
使用的方法
以下是完成此任務的各種方法
使用For迴圈和append()函式(字典遍歷方法)
使用dict.keys()函式
使用For迴圈、keys()和values()函式
示例
假設我們已經獲得了一個輸入字典。我們現在將使用上述方法列印輸入字典中每個專案的鍵和值對的和。
輸入
inputDict = {5: 3, 10: 2, 6: 4, 12:8}
輸出
8 12 10 20
在上述輸入字典中,每個專案的鍵和值的和為:
第一項 − 5+3 = 8
第二項 − 10+2=12
第三項 − 6+4=10
第四項 − 12+8 = 20
因此,我們得到輸出為8 12 10 20。
使用For迴圈和append()函式
在這種方法中,我們將使用簡單的for迴圈和append函式的組合來列印字典中所有鍵值對的和。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個函式getKeyValuesSum(),透過接受輸入字典作為引數來列印字典的鍵值對的和。
初始化一個空列表來儲存每個字典項的鍵和值的和。
使用for迴圈遍歷輸入字典的每個項。
將字典的鍵和值相加,並將其新增到結果列表中。
遍歷結果並列印鍵值對的和。
建立一個變數來儲存輸入字典。
透過將輸入字典作為引數呼叫上面定義的getKeyValuesSum()函式,然後列印它。
示例
下面的程式使用for迴圈和append()函式返回輸入字典中每個專案的鍵和值對的和:
# creating a function that prints the sum of key and value pairs
# of a dictionary by accepting input dictionary as an argument
def getKeyValuesSum(inputDict):
# Creating a list to store sum of keys and values of each dictionary item
resultantList = []
# traversing through each item of the input dictionary
for item in inputDict:
# Appending the key(item) as well as the value(inputDict[item]) to the result
resultantList.append(item + inputDict[item])
print("The Sum of key and value pairs of the dictionary is:")
# Printing the resultant list
for i in resultantList:
print(i,end=' ')
# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
# Printing input dictionary
print("The Given Dictionary:", inputDict)
# calling the above defined getKeyValuesSum() function
# by passing input dictionary as an argument
getKeyValuesSum(inputDict)
輸出
執行上述程式後,將生成以下輸出:
The Given Dictionary: {5: 3, 10: 2, 6: 4, 12: 8}
The Sum of key and value pairs of the dictionary is:
8 12 10 20
使用dict.keys()函式
在這種方法中,我們將使用keys()函式,dict.keys()方法提供一個檢視物件,按插入順序顯示字典中所有鍵的列表。
示例
下面的程式使用keys()函式返回輸入字典中每個專案的鍵和值對的和:
def getKeyValuesSum(inputDict):
# storing the sum of keys and values of each dictionary item
resultantList = []
# traversing through the keys of the input dictionary
for k in inputDict.keys():
# Appending the key as well as the value(inputDict[key]) to the result
resultantList.append(k + inputDict[k])
# Print the values of the resultant list
print(resultantList)
# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
getKeyValuesSum(inputDict)
輸出
執行上述程式後,將生成以下輸出:
[8, 12, 10, 20]
使用For迴圈、keys()和values()函式
在這種方法中,我們將使用keys()和values()函式來列印Python字典中所有鍵值對的和。
語法
keys() function:
dict.keys()方法提供一個檢視物件,按插入順序顯示字典中所有鍵的列表。
values() function:
dict.values()方法提供一個檢視物件,按插入順序顯示字典中所有值的列表。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存輸入字典。
使用dict.keys()函式獲取輸入字典的所有鍵的列表。
使用dict.values()函式獲取輸入字典的所有值的列表。
初始化一個空列表來儲存每個字典項的鍵和值的和。
使用for迴圈在使用len()函式(返回物件中的專案數)的鍵列表長度範圍內進行遍歷。
獲取鍵和值列表中當前元素的和,並使用append()函式將其新增到結果列表中。
列印結果列表。
示例
下面的程式使用For迴圈、keys()和values()函式返回輸入字典中每個專案的鍵和值對的和:
# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
# getting a list of keys of an input dictionary
keysList = list(inputDict.keys())
# getting a list of values of an input dictionary
valuesList = list(inputDict.values())
# storing the sum of keys and values of each dictionary item in a list
resultantList = []
# traversing in a range till the length of the keys list
for p in range(0, len(keysList)):
# appending the sum of current elements in both keys and value list and
resultantList.append(keysList[p]+valuesList[p])
for e in resultantList:
# printing current element
print(e, end=" ")
輸出
執行上述程式後,將生成以下輸出:
8 12 10 20
結論
在這篇文章中,我們學習瞭如何使用三種不同的方法列印字典中所有鍵值對的和。我們還學習瞭如何使用keys()和values()函式分別獲取所有鍵和字典值。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP