[英]How to find maximum value in a list of arrays
I have a very large list of arrays that looks something like this: 我有一个非常大的数组列表,看起来像这样:
'apples': array([1,2,3,4]), 'pears': array([1,2,10,11]),'oranges': array([1,10,10,3])
I want to find the maximum value out of all the arrays. 我想找到所有数组中的最大值。 In my short example above it would be 11.
在我上面的简短示例中,它将是11。
I am struggling to write something that would give me the maximum value for all arrays in the list. 我正在努力写一些能给我列表中所有数组最大值的东西。 Any thoughts would be welcome.
任何想法都会受到欢迎。
Your list is actually a dictionary, so you can do this: 你的列表实际上是一本字典,所以你可以这样做:
arr = {'apples': [1,2,3,4], 'pears': [1,2,10,11],'oranges': [1,10,10,3]}
res = max(map(max, arr.values()))
print(res)
Output: 输出:
11
You can also do it using the chain
function from itertools
module: 您也可以使用
itertools
模块中的chain
函数来完成它:
from itertools import chain
arr = {'apples': [1,2,3,4], 'pears': [1,2,10,11],'oranges': [1,10,10,3]}
res = max(chain(max(i) for i in arr.values()))
print(res)
Output: 输出:
11
would this work? 这会有用吗?
big_dict = {"apple" : list([1,2,3]),
"pear" : list([4,5,6])}
print max([max(v) for v in big_dict.itervalues()])
From your example given it looks like that's a dictionary rather than a list, anyways for simplicity let's assume that it is a list in the form of: 从你给出的例子来看,它看起来像是字典而不是列表,但为了简单起见,我们假设它是一个以下形式的列表:
[[2,3,55],[23,5,63],[2,3,6],[4,6,78], ...]
So what you can do is iterate over the main list and keep the current max value in a variable, and just compare to max of each array and update if greater. 因此,您可以做的是遍历主列表并将当前最大值保留在变量中,并且只比较每个数组的最大值并更新(如果更大)。
Example 例
max_value = 0
for l in yourList:
curr = max(l)
if curr > max_value:
max_value = curr
print max_value
This can be easily converted to use dictionary instead of a list, just iterate over each of the items within the dictionary and the logic would be the same (would have to get max()
from the value). 这可以很容易地转换为使用字典而不是列表,只是迭代字典中的每个项目,逻辑将是相同的(必须从值获得
max()
)。
Hope this helps! 希望这可以帮助!
If your data is in a dictionary variable d, then give this a try 如果您的数据在字典变量d中,请尝试一下
l=[max(d[key]) for key in d.keys()]
print max(l)
Or you can use a one liner 或者你可以使用一个班轮
print max([max(d[key]) for key in d.keys()])
你不能直接这样做,尝试形成另一个数组并检查'max(arraylist)'
First create a new list that contains the max values from each dictionary element then get the max of that new list 首先创建一个新列表,其中包含每个词典元素的最大值,然后获取该新列表的最大值
dict = {"apples" : [1, 2, 8, 4], "fruits": [3,2,6]}
maxlst = [max(el) for el in dict.values()]
print(max(maxlst))
The more pythonic way for me is to use comprehension dicts : 对我来说,更加pythonic的方式是使用理解词:
dictionary = {'apples': np.array([1,2,3,4]),
'pears': np.array([1,2,10,11]),'oranges': np.array([1,10,10,3])}
In [144]: { k:v.max() for k,v in dictionary.items() }
Out[144]: {'apples': 4, 'oranges': 10, 'pears': 11}
An other easy way is to use pandas : 另一种简单的方法是使用熊猫:
In [145]: pd.DataFrame(dictionary).max()
Out[145]:
apples 4
oranges 10
pears 11
dtype: int32
You can have something like below, for large list 对于大型列表,您可以使用下面的内容
obj={'apples': [1,2,3,4], 'pears': [1,2,10,11],'oranges': [1,10,10,3]}
max(sum(obj.values(), []))
11
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.