输出元素在特定位置具有最大值。蟒蛇

时间:2013-10-10 19:08:59

标签: python append max output

我在这里尝试做的是追加元素形式 the_list 最大值

[ - 1] 位置。我开始为the_list中的元素创建一个索引字典,但我开始迷失在逻辑流程中。

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])

最后我想:

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

因为4大于1和2

1 个答案:

答案 0 :(得分: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']
相关问题