0

what I am trying to do here is append the element form the_list with the greatest value

in the [-1] position . i started by creating an index dictionary for the elements in the_list but i started to get lost in the logic flow.

the_list = [['a','b','c','1'],['b','c','e','4'],['d','e','f','2']]
D_indx_element = {}
D_indx_value = {}
output = []

temp = []
for i,k in zip(range(0,len(the_list)),the_list):
    D_indx_element[i] = k
    temp.append(int(k[-1]))
    D_indx_value[i] = int(k[-1])

in the end i would like to have:

output = [['b','c','e','4']]

since 4 is greater than 1 and 2

4

1 回答 1

1

使用max

>>> the_list = [['a','b','c','1'],['b','c','e','4'],['d','e','f','2']]
>>> max(the_list, key=lambda x:int(x[-1]))
['b', 'c', 'e', '4']

没有lambda

def func(x):
    return int(x[-1])
max(the_list, key=func)
#['b', 'c', 'e', '4']
于 2013-10-10T19:10:19.933 回答