簡體   English   中英

如何使用循環從列表中找到三個最大值?

[英]How to find the three maximum value from a list using loop?

我有以下字典,我想從中獲取最后一個值的 3 個最大值。 從三個。 我應該如何使用循環 go 關於它?

G0001NB  :  ['Fredericton Gardens', '264.813134990644', 23833.18214915796]
G0002NB  :  ['Saint John Flowerbeds', '308.65432870469186', 27778.889583422268]
G0001NS  :  ['Halifax Gardens', '318.94189777177445', 28704.7707994597]
G0001PE  :  ['Charlottetown Gardens', '361.21898123683434', 32509.70831131509]
G0002PE  :  ['Summerside Meadows', '264.95778233234654', 23846.200409911187]
G0001NL  :  ['St.Johns Flowers', '524.3613531477152', 47192.521783294374]
G0002NL  :  ['Flowers in Labrador', '400.6868146749942', 36061.813320749476]
G0001QC  :  ['Fleur de Montreal', '261.0589768948249', 23495.307920534244]
G0001ON  :  ['Ottawa Fields', '325.5476915528243', 29299.292239754184]
G0002ON  :  ['Toronto Meadows', '275.4492991562006', 24790.436924058053]
G0003ON  :  ['London Gardens', '301.3513495675443', 27121.621461078983]
G0001MB  :  ['Winnipeg Meadows', '117.43417931323137', 10569.076138190823]
G0001SK  :  ['Regina Gardens', '307.6230292454801', 27686.072632093208]
G0002SK  :  ['Saskatoon Gardens', '354.3851315502206', 31894.661839519853]
G0001AB  :  ['Calgary Fields', '141.08265897062734', 12697.43930735646]
G0002AB  :  ['Edmonton Meadows', '351.50216826137427', 31635.195143523684]
G0001BC  :  ['Vancouver Meadows', '332.59825321044787', 29933.842788940306]
G0002BC  :  ['Victoria Gardens', '173.34623973921785', 15601.161576529606]
G0001YT  :  ['Whitehorse Fields', '355.09873981736985', 31958.886583563286]
G0001NT  :  ['Yellowknife Plot', '149.09626155613265', 13418.663540051939]
G0001NU  :  ['Iqaluit Greenhouse', '291.6230202078498', 26246.07181870648]

嘗試這樣的事情:

dictionary = {False: [1, 2, 10],
              10: ["", 2, 20],
              1: [1, "", 30],
              20: [1, "", 1]}
values = [i[-1] for i in dictionary.values()]
values.sort(reverse=True)
output = sum(values[:3])

Output:60

我讓它工作了。 我首先使用 list.sort() 對列表進行排序,然后打印 list[-n:]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM