-1
我正在迭代10,000個項目的列表。對於每件商品,我都會處理它並獲得價值。我想返回一個名稱和前10個值的元組列表,按值的降序排列。如何在Python中保留for循環的頂級結果列表
它看起來是這樣的:
top_tuples = []
for item in itemlist:
cur_value = compute_value(item)
my_tuple = (item, cur_value)
if cur_value is > the smallest value on my list:
remove smallest value from top_tuples
add tuple to top_tuples at appropriate index #index is based on value
謝謝。
你能告訴我們如何在一個項目的樣子? – 2017-08-01 14:39:16
您需要在'for'循環中使用另一個循環來遍歷當前的「最佳」列表。 –
可能分開處理和排序。第一個過程;然後進行排序(使用[排序內置函數](https://wiki.python.org/moin/HowTo/Sorting)) – mquantin