-2

可能重复:
数组列表的python max

我有一个数组列表,例如:

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

我试过max(a)返回以下错误:

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

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

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

我不想将内部数组转换为列表,因为列表的大小非常大。我也有目的地创建这样来执行数组操作。

4

3 回答 3

1

在您的示例中,您可以执行以下操作:

max(a, key=lambda i: i[0][0])

取决于您想要什么结果(用于排序的键),您可能必须“玩”索引。

http://docs.python.org/2/library/functions.html#max

于 2012-11-05T09:58:08.697 回答
0

无论如何,不​​知道你以前的答案是如何不够的,但是如果你正在处理 numpy 数组,那么为什么不是整个数组,而不是数组列表......然后只需使用适当的 numpy 函数:

from numpy import array

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

print a.max(axis=0)
#[[8 3]
# [8 3]]
于 2012-11-05T09:57:03.570 回答
0

关于什么:

max(a, key=lambda x:np.max(x))
# array ([[8, 3], [8, 3]])

请注意:第一个最大值是“正常最大值”,第二个是 np 的最大值......这里的逻辑是 max 使用 np.max 作为比较的关键,np.max 返回数组中的最高数字。

那是你要的吗?

于 2012-11-05T10:01:17.147 回答