簡體   English   中英

需要 select 對應列表中的最大值

[英]Need to select corresponding Maximum values from the list of lists

我有一個像[('0', 0.9999883), ('1', 0.00092986226), ('2', 0.00052163005), ('3', 0.00080531836)]這樣的繼續列表,需要 select 對應的最大值。 ..我們如何迭代?

對最大值進行簡單的線性搜索:

my_list = [('0', 0.9999883), ('1', 0.00092986226), ('2', 0.00052163005), ('3', 0.00080531836)]
max_value = 0
max_index = 0
for e in my_list:
    if e[1] > max_value:
        max_value = e[1]
        max_index = e[0]
print(max_index) #0
print(max_value) #0.9999883

你想要這樣的東西:

>>> lst = [('0', 0.9999883), ('1', 0.00092986226), ('2', 0.00052163005), ('3', 0.00080531836)]
>>> max(lst,key=lambda x:x[1])
('0', 0.9999883)

如果你只想要字符串部分,你可以這樣做:

>>> max(lst,key=lambda x:x[1])[0]
'0'

您已將列表放在文字中,因此如果它們在字符串中,您需要這樣的內容:

>>> lst = "[('0', 0.0015696287), ('1', 0.0053164363), ('2', 0.0008648634), ('3', 0.999303)]", "[('0', 0.00064742565), ('1', 0.030319422), ('2', 0.9896703), ('3', 0.004443854)]", "[('0', 0.99982405), ('1', 0.00092729926), ('2', 0.001509279), ('3', 0.00065752864)]", "[('0', 0.0030318499), ('1', 0.01600933), ('2', 0.9892292), ('3', 0.028750658)]", "[('0', 0.9989308), ('1', 0.0029929578), ('2', 0.0014030635), ('3', 0.0016490221)]", "[('0', 0.0027604103), ('1', 0.009357661), ('2', 0.0014455616), ('3', 0.99934363)]"

>>> [max(eval(inner_lst),key=lambda x:x[1]) for inner_lst in lst]
[('3', 0.999303), ('2', 0.9896703), ('0', 0.99982405), ('2', 0.9892292), ('0', 0.9989308), ('3', 0.99934363)]

或者,如果您喜歡:

>>> [max(eval(inner_lst),key=lambda x:x[1])[0] for inner_lst in lst]
['3', '2', '0', '2', '0', '3']

如果它們不在字符串中,而它們只是在列表中:

>>> lst = [[('0', 0.0015696287), ('1', 0.0053164363), ('2', 0.0008648634), ('3', 0.999303)], [('0', 0.00064742565), ('1', 0.030319422), ('2', 0.9896703), ('3', 0.004443854)], [('0', 0.99982405), ('1', 0.00092729926), ('2', 0.001509279), ('3', 0.00065752864)], [('0', 0.0030318499), ('1', 0.01600933), ('2', 0.9892292), ('3', 0.028750658)], [('0', 0.9989308), ('1', 0.0029929578), ('2', 0.0014030635), ('3', 0.0016490221)], [('0', 0.0027604103), ('1', 0.009357661), ('2', 0.0014455616), ('3', 0.99934363)]]
>>> print([max(inner_lst,key=lambda x:x[1])[0] for inner_lst in lst])
['3', '2', '0', '2', '0', '3']

假設您想要使用第二個值的最大元組:

>>> from operator import itemgetter
>>> l = [('0', 0.9999883), ('1', 0.00092986226), ('2', 0.00052163005), ('3', 0.00080531836)]
>>> max(l, key=itemgetter(1))
('0', 0.9999883)

如果您只想返回第一個項目:

>>> max(l, key=itemgetter(1))[0]
'0'

以上使用operator.itemgetter將 select 第二項作為最大key

嘗試這個,

>>> your_list = [('0', 0.9999883), ('1', 0.00092986226), ('2', 0.00052163005), ('3', 0.00080531836)]
>>> max(your_list , key= lambda x: x[1])
('0', 0.9999883) # output

這應該為您解決問題:

a = [('0', 0.9999883), ('1', 0.00092986226), ('2', 0.00052163005), ('3', 0.00080531836)]

tmp0, tmp1 = a[0][0], a[0][1]

for i,j in a:
    if tmp1 < j:
        tmp0 = i
        tmp1 = j

print(tmp0, tmp1)

暫無
暫無

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

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