[Numpy-discussion] List of arrays failing index(), remove() etc

Eric Firing efiring at hawaii.edu
Wed May 7 20:30:14 EDT 2014


On 2014/05/07 2:14 PM, mfm24 wrote:
> I'm having a problem I haven't seen elsewhere (and apologies if it has
> been answered before).
>
> I see the following behavior (copied verbatim from a python session):
>
> Python 2.7.4 (default, Apr  6 2013, 19:55:15) [MSC v.1500 64 bit (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import numpy as np
>>>> x=[[np.zeros(10)] for i in range(10)]
>>>> x.index(x[0])
> 0
>>>> x.index(x[1])
> Traceback (most recent call last):
>    File "", line 1, in
> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>>> x[1].append(np.zeros(10))
>>>> x.index(x[1])
> 1
>
> Any ideas why I see a ValueError when trying to find the index of a list
> containing a single ndarray?

In the first example, indexing with 0, it checks the first entry in x, 
finds that it *is* the target, and so returns the first index, 0.

In the second case, indexing with 1, it checks the first entry in x, 
finds that it is *not* the same object, so it checks to see if it has 
the same contents.  This leads it to compare two ndarrays for equality, 
which leads to the ValueError.

Eric

>
> -Matt
> ------------------------------------------------------------------------
> View this message in context: List of arrays failing index(), remove()
> etc
> <http://numpy-discussion.10968.n7.nabble.com/List-of-arrays-failing-index-remove-etc-tp37544.html>
> Sent from the Numpy-discussion mailing list archive
> <http://numpy-discussion.10968.n7.nabble.com/> at Nabble.com.
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>




More information about the NumPy-Discussion mailing list