Python 从带有列表的列表中获取最大值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33269530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get max value from a list with lists?
提问by FeatherMarauder
So I have a list that contains several list which all have three strings first, then one float number, like:
所以我有一个包含几个列表的列表,这些列表首先有三个字符串,然后是一个浮点数,例如:
resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]]
How do I make a function that returns the maximum value (which here would be 9.1931)? I tried
我如何制作一个返回最大值的函数(这里是 9.1931)?我试过
def MaxValue():
max_value = max(resultlist)
return max_value
but that just gives me a list.
但这只是给了我一个清单。
EDIT: Also, any way I could get the index for where the value comes from? Like, from which sublist?
编辑:另外,我可以通过什么方式获得价值来源的索引?比如,来自哪个子列表?
采纳答案by wflynny
Loop through your outer list and select the last element of each sublist:
循环遍历外部列表并选择每个子列表的最后一个元素:
def max_value(inputlist):
return max([sublist[-1] for sublist in inputlist])
print max_value(resultlist)
# 9.1931
It's also best if you keep all function related variables in-scope (pass the list as an argument and don't confuse the namespace by reusing variable names).
如果您将所有与函数相关的变量保留在范围内(将列表作为参数传递并且不要通过重用变量名称混淆命名空间),那也是最好的。
回答by bourbaki4481472
Are you trying to just get the maximum number from the floats (the last index in your list)? If so, here's a solution.
您是否想从浮点数(列表中的最后一个索引)中获取最大数量?如果是这样,这里有一个解决方案。
last_indices = [x[3] for x in resultlist]
return max(last_indices)
回答by LetzerWille
resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]]
print(max(map(lambda x: x[-1],resultlist)))
Output:
输出:
9.1931
回答by Almog
In perhaps a more functional than pythonic manner:
也许比pythonic方式更实用:
>>> max(map(lambda x: x[3], resultlist))
9.1931
It begins by mapping each element of result list to the number value and then finds the max.
它首先将结果列表的每个元素映射到数值,然后找到最大值。
The intermediate array is:
中间数组是:
>>> map(lambda x: x[3], resultlist)
[8.3931000000000004, 6.3231000000000002, 9.1930999999999994]
回答by Padraic Cunningham
If you want the index too you can use enumerate
with operator.itemgetter
using map
:
如果你想索引也可以用enumerate
与operator.itemgetter
使用map
:
from operator import itemgetter
def max_val(l, i):
return max(enumerate(map(itemgetter(i), l)),key=itemgetter(1)))
Which will return a tuple of the max with the index:
这将返回一个带有索引的最大值的元组:
In [7]: resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]]
In [8]: max_val(resultlist, -1)
Out[8]: (2, 9.1931)
Or just a regular gen exp:
或者只是一个普通的 gen exp:
from operator import itemgetter
def max_val(l, i):
return max(enumerate(sub[i] for sub in l), key=itemgetter(1))
回答by Cleb
Here is an answer just in case you get a list of list where the number is not always on the 3rd position:
这是一个答案,以防万一您得到一个数字并不总是在第三位的列表:
from itertools import chain
max(filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(resultlist)))
What is going on? itertools.chain
flattens the list of lists, the filter
then selects all the numeric values of which the maximal value is then determined using the max
function.
Advantage here is that it also works for arbitrary lists of lists where the numeric value can be found anywhere in the list.
到底是怎么回事?itertools.chain
展平列表列表,filter
然后选择所有数值,然后使用该max
函数确定最大值。这里的优点是它也适用于任意列表列表,其中数值可以在列表中的任何位置找到。
For your example:
对于您的示例:
resultlist = [['1', '1', 'a', 8.3931], ['1', '2', 'b', 6.3231], ['2', '1', 'c', 9.1931]]
max(filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(resultlist)))
#prints 9.1931
One more general example:
一个更一般的例子:
myList = [[23, 34, 'a'],['b'],['t', 100]]
max(filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(myList)))
#prints 100
EDIT:
编辑:
If you also want to get the index of the maximal value, you can do the following (using @Padraic Cunningham approach):
如果您还想获得最大值的索引,您可以执行以下操作(使用@Padraic Cunningham 方法):
from itertools import chain
import operator
resultlist = [['1', '1', 'a', 8.3931], ['1', '2', 'b', 6.3231], ['2', '1', 'c', 9.1931]]
l = filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(resultlist))
# l: [8.3931, 6.3231, 9.1931]
max(enumerate(l), key = operator.itemgetter(1))
#(2, 9.1931)
This approach assumes that there is exactly one numeric value per list!
这种方法假设每个列表只有一个数值!
One more example using a list where the numeric value is on an arbitrary position:
另一个使用列表的示例,其中数值位于任意位置:
from itertools import chain
import operator
myList = [[23, '34', 'a'],['b', 1000],['t', 'xyz', 100]]
l=filter(lambda x: isinstance(x, (int, long, float)), chain.from_iterable(myList))
max(enumerate(l), key = operator.itemgetter(1))
#prints (1, 1000)
回答by Guillermo Luijk
Numpy helps with numerical nested lists. Try this:
Numpy 有助于数字嵌套列表。尝试这个:
resultlist = [[3, 2, 4, 4], [1, 6, 7, -6], [5, 4, 3, 2]]
max(resultlist) # yields [5, 4, 3, 2] because 5 is the max in: 3, 1, 5
np.max(resultlist) # yields 7 because it's the absolute max
max()
returns the list which first element is the maximum of all lists' first element, while np.max()
returns the highest value from all the nested lists.
max()
返回第一个元素是所有列表第一个元素的最大值的列表,同时np.max()
返回所有嵌套列表中的最大值。