Python程式查詢字典鍵的總和


在這篇文章中,我們將學習一個Python程式來查詢字典鍵的總和。

使用的方法

以下是完成此任務的各種方法 -

  • 使用for迴圈

  • 使用dict.keys()和sum()函式

  • 使用reduce()函式

  • 使用items()函式

示例

假設我們已經獲取了一個包含一些隨機鍵值對的**輸入字典**。我們現在將使用上述方法查詢輸入字典中所有**鍵**的**總和**。

輸入

inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}

輸出

Sum of keys of an input dictionary: 25

在上面的示例中,輸入字典的鍵的總和為**4+1+10+2+8 = 25。**

方法1:使用For迴圈

演算法(步驟)

以下是執行所需任務的演算法/步驟 -。

  • 建立一個變數來儲存輸入字典。

  • 列印輸入字典。

  • 初始化一個**keysSum**變數為0,用於儲存輸入字典鍵的總和。

  • 使用**for迴圈**遍歷輸入字典的鍵。

  • 使用+運算子將當前鍵新增到keysSum變數。

  • 列印輸入字典鍵的總和。

示例

以下程式使用for迴圈返回輸入字典中所有鍵的總和 -

# input dictionary
inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}

# printing the input dictionary
print("Input Dictionary: ", inputDict)

# storing the sum of keys of the input dictionary
keysSum = 0

# traversing through the keys of an input dictionary
for k in inputDict:

   # adding the current key to keysSum variable
   keysSum += k

# printing the sum of keys of an input dictionary
print("Sum of keys of an input dictionary:", keysSum)

輸出

執行後,上述程式將生成以下輸出 -

Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
Sum of keys of an input dictionary: 25

方法2:使用dict.keys()和sum()函式

**dict.keys()函式** - 提供一個檢視物件,按插入順序顯示字典中所有鍵的列表

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 使用**dict.keys()**函式獲取輸入字典的鍵,並使用**list()**函式將其轉換為列表(返回可迭代物件的列表)。

  • 使用**sum()**函式獲取字典鍵的總和(返回可迭代物件中所有專案的總和)。

  • 列印輸入字典中所有鍵的總和。

示例

以下程式使用dict.keys()和sum()函式返回輸入字典中所有鍵的總和 -

# input dictionary
inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
print("Input Dictionary: ", inputDict)

# getting the keys of the input dictionary and converting them into the list

# and then getting the sum of dictionary keys
keysSum = sum(list(inputDict.keys()))
print("Sum of keys of an input dictionary:", keysSum)

輸出

執行後,上述程式將生成以下輸出 -

Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
Sum of keys of an input dictionary: 25

方法3:使用reduce()函式

reduce()函式

在Python中,reduce()函式迭代列表或其他可迭代資料型別中的每個專案,返回單個值。它位於functools庫中。這比迴圈更有效。

語法

reduce(function, iterable)

Lambda函式

Lambda函式,通常稱為“**匿名函式**”,與普通Python函式相同,只是它可以**無需名稱**地定義。**def**關鍵字用於定義普通函式,而**lambda**關鍵字用於定義匿名函式。但是,它們僅限於一行表示式。與常規函式一樣,它們可以接受多個引數。

語法

lambda arguments: expression

演算法(步驟)

以下是執行所需任務的演算法/步驟 -

  • 使用import關鍵字從functools模組匯入reduce函式。

  • 使用lambda函式新增連續的字典鍵,並減少大小以生成單個元素(所有鍵的總和)。

  • 列印輸入字典中所有鍵的總和。

示例

以下程式使用reduce()函式返回輸入字典中所有鍵的總和 -

# importing reduce function from functools module
from functools import reduce
inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
print("Input Dictionary: ", inputDict)

# Add the keys and reduce it to a single number(sum) using reduce() function
keysSum = reduce(lambda p, q: p + q, inputDict)
print("Sum of keys of an input dictionary:", keysSum)

輸出

執行後,上述程式將生成以下輸出 -

Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
Sum of keys of an input dictionary: 25

方法4:使用items()函式

使用**dictionary.items()**函式使用for迴圈遍歷字典中的元組。

**items()**函式返回一個檢視物件,即它包含字典的鍵值對,作為列表中的元組。

示例

以下程式使用items()函式和for迴圈返回輸入字典中所有鍵的總和 -

inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
print("Input Dictionary: ", inputDict)

# storing the sum of keys of the input dictionary
keysSum = 0

# traversing through the keys, and values of an input dictionary
for key, value in inputDict.items():
   
   # adding the current key to keysSum variable
   keysSum += key
print("Sum of keys of an input dictionary:", keysSum)

輸出

執行後,上述程式將生成以下輸出 -

Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
Sum of keys of an input dictionary: 25

結論

在本文中,我們學習了4種不同的計算字典鍵總和的方法。我們還學習瞭如何使用lambda函式和reduce向可迭代專案新增條件。

更新於: 2023年1月27日

3K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告