-2

Possible Duplicate:
python max of list of arrays

I have a list of arrays like:

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

I tried max(a) which returns the following error:

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],[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.

Community
  • 1
  • 1
Thiru
  • 3,293
  • 7
  • 35
  • 52

3 Answers3

1

In your example you can do something like this:

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

Depends on what result do you want (what key to use for sorting) you probably have to 'play' with indexes.

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

alexvassel
  • 10,600
  • 2
  • 29
  • 31
0

Anyay, not sure how your previous answer wasn't sufficient, but if you're dealing with numpy arrays, then why isn't the whole lot an array, instead of a list of arrays... Then just use the appropriate numpy function:

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]]
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
0

what about:

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

please note: the fist max is 'normal max' and the second one is np's max... the logic here is that max uses np.max as it's key for comparison, np.max returns the highest number in the array.

is that what you want?

jcr
  • 1,015
  • 6
  • 18