Python 中列表和字典的公共鍵


在本文中,我們將學習如何在 python 中查詢列表和字典中的公共鍵。

使用的方法

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

  • 使用“in”運算子和列表推導式

  • 使用 set(),intersection() 函式

  • 使用 keys() 函式和 in 運算子

  • 使用 Counter() 函式

示例

假設我們已經獲取了一個輸入字典和列表。我們將使用上述方法查詢輸入列表和字典鍵中的公共元素。

輸入

inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
inputList = ["hello", "tutorialspoint", "python"]

輸出

Resultant list: ['hello', 'tutorialspoint']

在上面的示例中,“hello”和“tutorialspoint”是輸入列表和字典鍵中的公共元素。因此,它們被打印出來。

方法 1:使用“in”運算子和列表推導式

列表推導式

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

Python“in”關鍵字

in 關鍵字以兩種方式工作 -

  • in 關鍵字用於確定一個值是否存在於一個序列(列表、範圍、字串等)中。

  • 它也用於在 for 迴圈中迭代一個序列

演算法(步驟)

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

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

  • 建立另一個變數來儲存輸入列表

  • 遍歷輸入列表並檢查任何輸入列表元素是否與使用列表推導式的字典鍵匹配。

  • 列印結果列表。

示例

以下程式使用“in”運算子和列表推導式返回輸入列表和字典鍵中的公共元素 -

# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}

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

# input list
inputList = ["hello", "tutorialspoint", "python"]

# printing input list
print("Input list:", inputList)

# checking whether any input list element matches the keys of a dictionary
outputList = [e for e in inputDict if e in inputList]

# printing the resultant list
print("Resultant list:", outputList)

輸出

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

Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']

方法 2:使用 set(),intersection() 函式

set() 函式 - 建立一個集合物件。集合列表將以隨機順序顯示,因為專案沒有排序。它刪除所有重複項。

intersection() 函式 - intersection() 方法返回一個包含兩個或多個集合之間相似性的集合。

這意味著僅包含同時存在於兩個集合中,或者如果比較了兩個以上的集合,則存在於所有集合中的專案。

示例

以下程式使用 set() 和 intersection() 返回輸入列表和字典鍵中的公共元素 -

# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}

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

# input list
inputList = ["hello", "tutorialspoint", "python"]

# printing input list
print("Input list:", inputList)

# Converting the input dictionary and input List to sets

# getting common elements in input list and input dictionary keys

# from these two sets using the intersection() function
outputList = set(inputList).intersection(set(inputDict))

# printing the resultant list
print("Resultant list:", outputList)

輸出

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

Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: {'hello', 'tutorialspoint'}

方法 3:使用 keys() 函式和 in 運算子

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

示例

以下程式使用 keys() 函式和 in 運算子返回輸入列表和字典鍵中的公共元素 -

# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}

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

# input list
inputList = ["hello", "tutorialspoint", "python"]

# printing input list
print("Input list:", inputList)

# empty list for storing the common elements in the list and dictionary keys
outputList = []

# getting the list of keys of a dictionary
keysList = list(inputDict.keys())

# traversing through the keys list
for k in keysList:
   
   # checking whether the current key is present in the input list
   if k in inputList:
      
      # appending that key to the output list
      outputList.append(k)
      
# printing the resultant list
print("Resultant list:", outputList)

輸出

Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']

方法 4:使用 Counter() 函式

Counter() 函式 - 一個計算可雜湊物件的子類。它在被呼叫/呼叫時隱式地建立可迭代物件的雜湊表。

這裡 Counter() 函式用於獲取輸入列表元素的頻率。

示例

以下程式使用 Counter() 函式返回輸入列表和字典鍵中的公共元素 -

# importing a Counter function from the collections module
from collections import Counter

# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}

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

# input list
inputList = ["hello", "tutorialspoint", "python"]

# printing input list
print("Input list:", inputList)

# getting the frequency of input list elements as a dictionary
frequency = Counter(inputList)

# empty list for storing the common elements of the list and dictionary keys
outputList = []

# getting the list of keys of a dictionary
keysList = list(inputDict.keys())

# traversing through the keys list
for k in keysList:

   # checking whether the current key is present in the input list
   if k in frequency.keys():
      
      # appending/adding that key to the output list
      outputList.append(k)
      
# printing the resultant list
print("Resultant list:", outputList)

輸出

Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']

結論

我們在本文中研究了四種不同的方法來顯示給定列表和字典中的公共鍵。我們還學習瞭如何使用 Counter() 函式獲取可迭代物件頻率的字典。

更新於: 2023-01-27

737 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告