Python程式:查詢字典中最大值元組的鍵


查詢Python字典中最大值元組的鍵,意味著識別與所有元組中值最高的元組關聯的鍵。這對於從資料中檢索最重要或最相關的資訊非常有用。

示例

假設我們已經輸入了一個字典,其值是元組。我們現在將使用上述方法查詢輸入字典中最大值元組的鍵。

輸入

inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}

輸出

The key of the maximum value in a tuple:  tutorialspoint

在上面的輸入字典中,元組中的最大值是15,其對應的鍵是tutorialspoint

使用的方法

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

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

  • 使用max()、values()和next()函式

方法1:使用列表推導式、max()和values()函式

在這種方法中,我們將瞭解如何使用列表推導式、max()和min()函式來查詢字典中最大值元組的鍵。

列表推導式

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

new_list = [expression for item in iterable if condition]

max()函式

max()方法(返回可迭代物件中最高值專案/最大數字)。

max_value = max(iterable)

dict.values()函式

dict.values()方法提供一個檢視物件,按插入順序顯示字典中所有值的列表。

value_list = dictionary.values()

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

演算法(步驟)

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

  • 建立一個變數來儲存包含元組作為值的輸入字典

  • 列印輸入字典。

  • 將給定的字典和鍵作為lambda函式傳遞,以查詢字典的最大值。

  • 使用items()函式和列表推導式迭代字典的鍵值對,獲取具有上述最大值的鍵。

  • 列印元組中最大值的字典鍵的結果。

示例

以下程式使用列表推導式、max()和values()函式返回元組中最大值的字典鍵

# input dictionary having values as tuples 
inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}
# printing input dictionary
print("Input dictionary:", inputDict)
# getting the maximum value from the values of the input dictionary 
# i.e, tuple 
maximumValue = max(inputDict.values(), key=lambda k: k[1])
# getting key with maximum value using list comprehension
maxKey = [k for k, v in inputDict.items() if v == maximumValue][0]
# printing the resultant dictionary key of the maximum value in a tuple
print("The key of the maximum value in a tuple: ", maxKey)

輸出

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

Input dictionary: {'hello': ('p', 2), 'tutorialspoint': ('c', 15), 'python': ('k', 8)}
The key of the maximum value in a tuple:  tutorialspoint

方法2:使用max()、values()和next()函式

此方法將演示如何在Python中使用max()、value()和next()函式來查詢字典中最大值元組的鍵。

lambda函式

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

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

語法

lambda arguments : expression

next()函式

返回迭代器(如列表、字串、元組等)中的下一個專案。如果可迭代物件到達其末尾,您可以新增預設返回值來返回。

語法

next(iterable, default)

演算法(步驟)

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

  • 建立一個變數來儲存包含元組作為值的輸入字典

  • 列印輸入字典

  • 透過將給定的字典和鍵作為lambda函式傳遞來獲取字典的最大值。

  • 使用next()函式遍歷給定的字典項,並使用上面的max變數查詢具有最大值的鍵。

  • 列印元組中最大值的字典鍵的結果。

示例

以下程式使用max()、values()和next()函式返回元組中最大值的字典鍵

# input dictionary having values as tuples 
inputDict = {'hello': ("p", 2), 'tutorialspoint': ("c", 15), 'python': ("k", 8)}
# printing input dictionary
print("Input dictionary:", inputDict)
# Getting maximum value from the dictionary using max() function
maximumValue = max(inputDict.values(), key=lambda k: k[1])
# Getting the maximum value key using the next() function
maxKey = next(k for k, v in inputDict.items() if v == maximumValue)
# printing the resultant dictionary key of the maximum value in a tuple
print("The key of the maximum value in a tuple: ", maxKey)

輸出

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

Input dictionary: {'hello': ('p', 2), 'tutorialspoint': ('c', 15), 'python': ('k', 8)}
The key of the maximum value in a tuple:  tutorialspoint

結論

在本文中,我們學習了兩種不同的方法來查詢字典中最大值元組的鍵。使用字典,我們學習瞭如何使用items(.)在字典的元素之間進行遍歷。此外,我們學習瞭如何使用next()函式向字典新增迭代器。最後但並非最不重要的一點是,我們學習瞭如何在max()函式中使用任何自定義函式作為鍵。

更新於:2023年8月17日

瀏覽量:154

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.