-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.

2
  • 7
    And your accepted answer from stackoverflow.com/questions/13227578/… isn't suitable...? Commented Nov 5, 2012 at 9:52
  • This post helped me to get what i need. Commented Nov 5, 2012 at 10:13

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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]]

1 Comment

This works exactly for my case. Thank you very much.
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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.