在列表列表中查找具有给定索引的最大值的列表,并返回其第一个值 [英] Finding a list in a list of lists with the max value by given index and returning its first value
问题描述
我有一个列表列表,我想通过列表中的一个索引找到最大值,在这种情况下为[5].
I have a list of lists and I want to find the max value by one index in the lists, 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]]
我如何让我的函数返回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]
的末尾添加 [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]
这篇关于在列表列表中查找具有给定索引的最大值的列表,并返回其第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!