Python 程式提取鍵數量最多的字典


Python 中,一個更常見地稱為關聯陣列的資料結構的實現是**字典**。字典由一組鍵值對組成。每個鍵值組合對應一個鍵及其對應的值。

在本文中,我們將學習一個 Python 程式來提取鍵數量最多的字典。

使用的方法

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

  • 使用 for 迴圈和 len() 函式

  • 使用列表推導式和 max() 函式

示例

假設我們已經獲取了一個包含多個字典的**輸入列表**。我們現在將使用上述方法提取具有最大鍵數的字典。

輸入

inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]

輸出

Resultant dictionary having maximum no of keys:
[{'hello': 5, 'tutorialspoint': 10, 'users': 15}]

在上面的示例中,第二個字典**{'hello': 5, 'tutorialspoint': 10, 'users': 15}**具有最大數量的鍵,即**3**。因此,它從輸入字典中提取。

使用 for 迴圈和 len() 函式

len() 方法返回物件中的專案數。當物件是字串時,len() 函式返回字串中的字元數。

演算法(步驟)

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

  • 建立一個變數來儲存包含**字典**的**輸入列表**。

  • 列印輸入列表。

  • 建立一個新的空字典來儲存結果字典。

  • 將最大長度初始化為 0 以儲存最大長度。

  • 使用**for 迴圈**遍歷輸入列表的每個元素。

  • 使用**if 條件**語句來檢查當前元素的長度是否大於使用**len()**函式(返回物件中的專案數)的最大長度。

  • 如果條件為真,則將該當前元素分配給結果字典。

  • 使用 len() 函式將當前元素的長度作為最大長度。

  • 列印輸入列表中鍵數量最多的結果字典。

以下程式使用 for 迴圈和 len() 函式返回輸入字典列表中鍵數量最多的字典。

# input list containing dictionaries
inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15},
   {'good': 3, 'website': 9}]
# printing input list
print("Input list of dictionaries:\n", inputList)
# creating a new empty dictionary
resultantDict = dict()
# initializing with 0 for storing maximum length
maxLength = 0
# traversing through each element of the input list
for p in inputList:
    # checking whether the length of the current element is
    # greater than the maximum length
    if len(p) > maxLength:
        # assigning that current element to the resultant dictionary
        resultantDict = p
		# assigning the length of a current element as the maximum length
        maxLength = len(p)
# printing resultant dictionary having a maximum no of keys in the input list
print("Resultant dictionary having maximum no of keys:\n", resultantDict)

輸出

執行上述程式將生成以下輸出。

Input list of dictionaries:
 [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]
Resultant dictionary having maximum no of keys:
 {'hello': 5, 'tutorialspoint': 10, 'users': 15}

使用列表推導式和 max() 函式

當您希望基於現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。

max() 函式(返回可迭代物件中值最高的專案/最大數)

演算法(步驟)

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

  • 建立一個變數來儲存包含**字典**的**輸入列表**。

  • 列印輸入列表。

  • 使用列表推導式遍歷輸入字典列表並獲取字典的長度。

  • 使用 max() 函式查詢輸入列表中字典的最大長度。

  • 使用列表推導式遍歷輸入字典列表並獲取具有此長度的字典。

  • 列印輸入列表中鍵數量最多的結果字典。

示例

以下程式使用列表推導式和 max() 函式返回輸入列表中鍵數量最多的字典。

# input list containing dictionaries
inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15},
    {'good': 3, 'website': 9}]
# printing input list of dictionaries
print("Input list of dictionaries:\n", inputList)
# getting the lengths of each dictionary element using list comprehension
# getting the maximum length from this list using the max() function
maxLength = max(len(p) for p in inputList)
# Finding the dictionary with the length as the maximum length
resultantDict = [p for p in inputList if len(p) == maxLength]
# printing resultant dictionary having maximum no. of keys in the input list
print("Resultant dictionary having maximum no of keys:\n", resultantDict)

輸出

執行上述程式將生成以下輸出。

Input list of dictionaries:
 [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]
Resultant dictionary having maximum no of keys:
 [{'hello': 5, 'tutorialspoint': 10, 'users': 15}]

結論

在本文中,我們學習瞭如何使用兩種不同的方法提取鍵數量最多的字典。為了快速解決問題,我們還學習瞭如何使用列表推導式以及簡單、簡潔的語法。

更新於: 2023年8月18日

89 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.