蟒蛇Max阵列的名单 [英] python max of list of arrays

查看:172
本文介绍了蟒蛇Max阵列的名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的阵列的列表:

I have a list of arrays like:

a = [array([6,2]),array([8,3]),array([4,2])]

我试过最大值(A)返回以下错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我希望它返回一个列表或数组,如:

I want it to return either a list or array like:

In: max(a)
Out: [8,3]

我不想要转换的内部数组列表,因为列表的大小是非常大的。此外,我故意这样创建的执行数组操作。

I don't want to convert the inner arrays to list, because the size of the list is very big. Also I purposefully created like that to perform array operations.

推荐答案

最简单的方法是将转换为元组/列表进行比较的缘故(或实现自己的比较):

The easiest way is to convert to tuple/lists for the sake of comparison (or implement the comparison yourself):

>>> max(a, key=tuple)
array([8, 3])

请注意,这是内建的最大,而不是 np.max

Note this is the builtin max and not np.max

编辑:

有关多维数组,使用 .tolist 方法:

For multi dimensional arrays, use the .tolist method:

max(a, key=operator.methodcaller('tolist'))

这篇关于蟒蛇Max阵列的名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆