Python程式查詢列表中存在鍵的字典中的最大值
在 Python 中,我們可以使用簡單的 for 迴圈以及 max、count 和 operator 函式來查詢列表中存在鍵的字典中的最大值。
Python 對資料結構的實現,通常被稱為關聯陣列,即 **字典**。字典由一組鍵值對組成。每個鍵值組合對應一個鍵及其對應的值。
示例
假設我們已經獲取了 **輸入字典** 和 **輸入列表**。現在我們將從輸入字典中查詢最大值,該字典的鍵也存在於輸入列表中。
輸入
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
inputList = ["python", "dictionaries", "article", 'codes']
輸出
Maximum value of dictionary where the key is in the list: 15
在上面的輸入字典中,**最大**值為 **20**,其對應的鍵為 **tutorialspoint**。但是單詞 **tutorialspoint** 不存在於輸入列表中。因此,檢查下一個最大值,即 **codes**,它也在列表中。
因此,輸出為 15。
使用 for 迴圈
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個變數來儲存 **輸入字典**。
建立另一個變數來儲存 **輸入列表**。
初始化一個變數 **(maxValue)** 為 0,用於儲存字典的結果最大值。
使用 **for 迴圈** 遍歷輸入列表的每個元素。
使用 **if 條件** 語句檢查當前元素是否存在於字典的鍵中。
使用 **max()** 函式從上面初始化的 **maxValue** 變數值和字典的當前鍵值中獲取最大值。
列印輸入字典的結果最大值,其中鍵存在於輸入列表中
示例
以下程式使用 for 迴圈和 max() 函式返回輸入字典中的最大值,其中鍵也存在於輸入列表中。
# input dictionary
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
# printing input dictionary
print("Input dictionary:", inputDict)
# input list
inputList = ["python", "dictionaries", "article", 'codes']
# intializing with 0 for storing resultant max value
maxValue = 0
# traversing through each element of the input list
for e in inputList:
# checking whether the current element is present in the keys of a dictionary
if e in inputDict:
# getting the max value from the maxValue variable and
# the current key value of the dictionary
maxValue = max(maxValue, inputDict[e])
# printing the resultant maximum value
print("Maximum value of dictionary where the key is in the list:", maxValue)
輸出
執行上述程式將生成以下輸出:
Input dictionary: {'hello': 6, 'tutorialspoint': 20, 'python': 5, 'codes': 15}
Maximum value of dictionary where the key is in the list: 15
使用列表推導式和 max() 函式
當您希望基於現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。
**max() 方法** - 返回可迭代物件中最高值項/最大數。
示例
以下程式使用列表推導式和 max() 函式返回輸入字典中的最大值,其中鍵也存在於輸入列表中。
# input dictionary
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
# input list
inputList = ["python", "dictionaries", "article", 'codes']
# Get all the elements Values that are present in both dictionary and the list
# Get the max of this list using the max() function
maxValue = max([inputDict[e] for e in inputList if e in inputDict])
# printing the resultant maximum value
print("Maximum value of dictionary where the key is in the list:", maxValue)
輸出
Maximum value of dictionary where the key is in the list: 15
使用 Counter() 函式
**Counter() 函式** - 一個子類,用於計算可雜湊物件。當被呼叫/執行時,它會隱式地建立一個可迭代物件的雜湊表。
在這種方法中,我們使用 **Counter() 函式** 將列表元素的頻率作為鍵值對獲取。
示例
以下程式使用 Counter() 函式返回輸入字典中的最大值,其中鍵也存在於輸入列表中。
# importing a Counter function from the collections module
from collections import Counter
# input dictionary
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
# input list
inputList = ["python", "dictionaries", "article", 'codes']
# getting the frequency of input list elements as a key-value pair
listFrequency = Counter(inputList)
# intializing with 0 for storing resultant max value
maxValue = 0
# traversing through each element of the input list
for p in inputDict:
# checking whether the current element is present in the keys
# of above list frequency
if p in listFrequency.keys():
# getting the max value from the maxValue variable
# and the current key value of dictionary
maxValue = max(maxValue, inputDict[p])
# printing the resultant maximum value
print("Maximum value of dictionary where the key is in the list:", maxValue)
輸出
Maximum value of dictionary where the key is in the list: 15
使用 operator.countOf() 方法
在這種方法中,我們將使用 Python 中 operator 庫的 coutof() 函式來查詢字典中的最大值。
語法
operator.countOf(a, b)
operator 模組的 countOf() 函式返回 **a** 中等於 **b** 的元素數量。
示例
以下程式使用列表推導式和 operator.countOf() 函式返回輸入字典中的最大值,其中鍵也存在於輸入列表中。
import operator as op
# input dictionary
inputDict = {"hello": 6, "tutorialspoint": 20, "python": 5, "codes": 15}
# input list
inputList = ["python", "dictionaries", "article", 'codes']
maxValue = max([inputDict[e]
for e in inputList if op.countOf(inputDict, e) > 0])
# printing the resultant maximum value
print("Maximum value of dictionary where the key is in the list:", maxValue)
輸出
Maximum value of dictionary where the key is in the list: 15
結論
可以使用本文介紹的四種不同技術之一來查詢列表中包含鍵的字典中的最大值。我們學習的另一個新方法是 operator.countOf() 函式,用於確定可迭代物件的元素計數。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP