[英]comparing more than one dict in python to get the maximum value for similar key
大家好,
我在解決以下問題時感到震驚,即比較 python 中的多個字典以獲得類似鍵的最大值。請您幫我解決此類問題。
注意:在 Stackoverflow 中提出了類似的問題,但這僅適用於兩個大小相同的字典,但在我的問題中,它完全不同。
樣本輸入:
a={"test1":90, "test2":45, "test4":74}
b={"test1":32, "test2":45, "test3":82, "test5":100}
c={"test1":78, "test2":65, "test3":92, "test4":90, "test5":90}
d={"test1":42, "test2":35, "test3":62, "test4":80}
樣品 Output:
res={"test1":90, "test2":65, "test3":92, "test4":90, "test5":100}
以下應該有效:
dicts = [a,b,c,d]
res ={}
for dic in dicts:
for key in dic.keys():
if key not in res.keys():
res[key] = dic[key]
if dic[key] > res[key]:
res[key] = dic[key]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.