7

I have a list of arrays like:

a = [array([6,2]),array([8,3]),array([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]

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.

Thiru
  • 3,293
  • 7
  • 35
  • 52
  • 3
    What defines the largest array? The sum of the array? The first element? – Aesthete Nov 05 '12 at 07:16
  • It can either be the sum of the array or the first element, anything will be useful/ applicable in my case. – Thiru Nov 05 '12 at 09:38
  • 1
    I don't think it's very helpful if you change your original question so much that any of the answers here become irrelevant. – Aesthete Nov 05 '12 at 09:44
  • Yes, that's true. But this is what i need now? Is it better to post the modified version as a new question? – Thiru Nov 05 '12 at 09:46
  • I think it's probably better to take some of the answers given here, and build your own solution. It's essentially the same question, just with an extra level of nesting. – Aesthete Nov 05 '12 at 09:56

4 Answers4

8

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

Note this is the builtin max and not np.max

EDIT:

For multi dimensional arrays, use the .tolist method:

max(a, key=operator.methodcaller('tolist'))
JBernardo
  • 32,262
  • 10
  • 90
  • 115
  • this works, but if my list is like this, then it returns the same ValueError, please help me! a = [array([[1,2],[2,2],[3,2]]),array([[4,3],[4,4],[5,4]])] – Thiru Nov 05 '12 at 08:10
  • I also need the index to be returned, please help me! – Thiru Nov 06 '12 at 08:06
  • 1
    @Alaissham Check the updated answer. You can also use a lambda as key: `lambda x: x.tolist()` – JBernardo Nov 06 '12 at 09:41
2

Defining max on arrays is, as it says in the exception, ambiguous. If we have the following arrays: [6, 2], [5, 1], then I guess the output should be [6, 2], but if we have the following example: [6, 2], [7, 1] what would then the output have to be.

In fact there are so many different definitions of max here. If we take the arrays to be vectors then max can be the magnitude of them, or the vector that has maximum x coord, etc. Max can just as well compare the length of the arrays in the list and return the one with most elements or in the case of a tie the first one of the ones with equal length.

My suggestion is to create a class abstracting your array structures and define the max operation there with exactly the outcome you expect.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
  • I will not get this kind of complexity, at least in my program, because my arrays are like [1.112,1.114,1.112],[2,321,2.309,2.317] and all the arrays have same length. – Thiru Nov 05 '12 at 08:17
1

To have the max array along with the idx, this is what i'm using now:

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

In: max_a = map(max, a)
Out: max_a = [6,8,4]

In: idx = max_a.index(max(max_a))
Out: idx = 1

In: result = a[idx]
Out: reusult = [8,3]
Thiru
  • 3,293
  • 7
  • 35
  • 52
0
     python 3.2

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

     Its working well
raton
  • 418
  • 5
  • 14
  • 2
    Your "answers" are of poor quality. They don't explain what is going on, sometimes don't reflect OP's code and often are not the best solution. You're posting them after a clearly superior solution has been proposed and accepted. Please think twice before doing so. – SilentGhost Nov 05 '12 at 21:08
  • That's working fine, but what i have is arrays inside the list. Can you help me to find the index of the max. – Thiru Nov 06 '12 at 08:17