在列表中查找列表的最大值,并返回单独的列表值python [英] Finding max of list in lists and returning a separate list value python
问题描述
因此,我有一个包含多个并行对象列表的列表,我想在列表中找到一个索引的最大值,在这种情况下为[5].
So I have a list which contains multiple parallel object lists and I want to find the max value of one index in the list, in this case [5].
一旦有了最大值,我将如何找到它的出现位置并返回该列表的第一个元素?
Once I have the max value, how would I find where that occurs and return the first element of that list?
例如,inputlist = [[70, 'Cherry St,', 43.684371, -79.316756, 23, 9, 14, True, True],
[78, 'Cow Rd', 43.683378, -79.322961, 15, 13, 2, True, False]]
For example, inputlist = [[70, 'Cherry St,', 43.684371, -79.316756, 23, 9, 14, True, True],
[78, 'Cow Rd', 43.683378, -79.322961, 15, 13, 2, True, False]]
我如何让我的函数返回78,因为它包含列表列表中index [5]的最大值?
How can I get my function to return 78 as it contains the maximum value of index[5] in my list of lists?
推荐答案
使用 max()
函数,但通过一个参数来检查特定的索引:
Use the max()
function, but pass through an argument to check a specific index:
max(inputList, key=lambda x: x[5])
这将返回inputList
中具有最大索引5值的子列表.如果要实际访问此值,请在语句末尾添加[5]
.
This returns the sublist in inputList
that has the greatest value at index 5. If you want to actually access this value, add [5]
to the end of the statement.
由于似乎您希望列表的ID包含最多5个元素,因此在末尾添加[0]
:
Since it seems that you want the ID of the list with the maximum 5th element, add [0]
to the end:
max(inputList, key=lambda x: x[5])[0]
这篇关于在列表中查找列表的最大值,并返回单独的列表值python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!