我有一本看起來像這樣的字典
a_dictionary = {"a": {"green_count: 12"}, "b": {"green_count: 13"}, "c": {"green_count: 2"}}
如何找到green_count最大值并回傳鍵“b”?
我知道如何對字典鍵內的值執行此操作,max_key = max(a_dictionary, key=a_dictionary.get但是我不知道如何對字典中的字典內的值執行此操作。
uj5u.com熱心網友回復:
假設您確實有一個嵌套字典(因此您的語法不正確):
a_dictionary = {"a": {"green_count": 12}, "b": {"green_count": 13}, "c": {"green_count": 2}}
max(a_dictionary, key=lambda x: a_dictionary[x].get('green_count',0))
輸出:'b'
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/434473.html
