Python程式從字典列表中提取具有給定鍵的字典


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

在本文中,我們將學習一個 Python 程式,用於從字典列表中提取具有給定鍵的字典。

使用的方法

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

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

  • 使用 filter() 和 lambda 函式

  • 使用 operator.countOf() 方法

示例

假設我們已經獲取了一個包含字典的輸入列表和一個輸入鍵。我們現在將使用上述方法從包含給定輸入鍵的列表中提取字典。

輸入

inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9},
   {'users': 12, 'users': 15, 'hello': 18},
   {'users': 21}]
inputKey = 'users'      

輸出

The resultant list of dictionaries containing the 'users' key −
[{'users': 15, 'hello': 18}, {'users': 21}]

在上述字典輸入列表中,只有**{'users': 12, 'users': 15, 'hello': 18}, {'users': 21}** 字典包含輸入鍵**users**。因此,這些字典是從輸入列表中提取的。

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

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

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

演算法(步驟)

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

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

  • 列印輸入列表。

  • 建立另一個變數來儲存輸入鍵。

  • 使用列表推導遍歷給定的字典列表,並使用 in 運算子檢查鍵是否在字典的鍵中。

  • 如果為真,則將此字典儲存在列表中。

  • 列印包含輸入鍵的結果字典列表。

示例

以下程式使用列表推導和 keys() 函式從輸入字典中返回包含輸入鍵的字典列表。

# input list of dictionaries
inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9},
   {'users': 12, 'users': 15, 'hello': 18},
   {'users': 21}]
# printing input list
print("Input List:\n", inputList)
# input key
inputKey = 'users'
# Traversing in the given input list and checking 
# whether the input Key exists in the list of dictionaries keys
resultantDict = [e for e in inputList if inputKey in list(e.keys())]
# printing resultant list of dictionaries containing input key
print("The Resultant list of dictionaries containing the 'users' key:\n", resultantDict)

輸出

Input List:
 [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}]
The Resultant list of dictionaries containing the 'users' key:
 [{'users': 15, 'hello': 18}, {'users': 21}]

使用 filter() 和 lambda 函式

**filter() 函式** - 使用一個函式過濾指定的序列,該函式確定序列中的每個元素是真還是假。

lambda 函式

lambda 函式是一個小的匿名函式。

lambda 函式可以有無限/任意數量的引數,但只有一個表示式。

語法

lambda arguments : expression

示例

以下程式使用 filter() 和 lambda 函式從輸入字典中返回包含輸入鍵的字典列表。

# input list of dictionaries
inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9},
   {'users': 12, 'users': 15, 'hello': 18},
   {'users': 21}]
# printing input list
print("Input List:\n", inputList)
# input key
inputKey = 'users'
# Filtering all the dictionaries with the given input key
resultantDict = list(
    filter(lambda sub: inputKey in list(sub.keys()), inputList))
# printing the resultant list of dictionaries containing input key
print("Resultant list of dictionaries containing 'users' key:\n", resultantDict)

輸出

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

Input List:
 [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}]
Resultant list of dictionaries containing 'users' key:
 [{'users': 15, 'hello': 18}, {'users': 21}]

使用 operator.countOf() 方法

operator 模組的 countOf() 函式返回**a**中等於**b**的元素數量。

語法

operator.countOf(a, b)

引數

  • a - 列表或字串或任何其他資料型別。

  • b - 我們必須在“a”中計算出現次數的值

示例

以下程式使用列表推導和 keys() 函式從輸入字典中返回包含輸入鍵的字典列表。

import operator as op
# input list of dictionaries
inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9},
   {'users': 12, 'users': 15, 'hello': 18},
   {'users': 21}]
# printing input list
print("Input List:\n", inputList)
# input key
inputKey = 'users'
# Traversing in the given input list and checking
# whether the input Key exists in the list of dictionaries keys
resultantDict = [e for e in inputList if op.countOf(
    list(e.keys()), inputKey) > 0]
# printing resultant list of dictionaries containing input key
print("The Resultant list of dictionaries containing the 'users' key:\n", resultantDict)

輸出

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

Input List:
 [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}]
The Resultant list of dictionaries containing the 'users' key:
 [{'users': 15, 'hello': 18}, {'users': 21}]

結論

在本文中,我們學習瞭如何使用三種不同的技術從字典列表中檢索具有特定鍵的字典。學習了新方法。使用 operator.countOf() 統計可迭代物件的元素。為了確定元素是否存在,可以使用新方法代替“in”和“not in”運算子。

更新於: 2023年8月18日

115 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告