Re: Rich Comparisons Gotcha



Robert Kern wrote:
James Stroud wrote:
py> 112 = [1, y]
py> y in 112
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is...

but not

py> ll1 = [y,1]
py> y in ll1
True

It's this discrepancy that seems like a bug, not that a ValueError is raised in the former case, which is perfectly reasonable to me.

Nothing to do with numpy. list.__contains__() checks for identity with "is" before it goes to __eq__().

....but only for the first element of the list:

py> import numpy
py> y = numpy.array([1,2,3])
py> y
array([1, 2, 3])
py> y in [1, y]
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type 'exceptions.ValueError'>: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
py> y is [1, y][1]
True

I think it skips straight to __eq__ if the element is not the first in the list. That no one acknowledges this makes me feel like a conspiracy is afoot.
.



Relevant Pages

  • Re: Rich Comparisons Gotcha
    ... Traceback: ... The truth value of an array with more than one element is... ... It's this discrepancy that seems like a bug, not that a ValueError is raised in the former case, which is perfectly reasonable to me. ...
    (comp.lang.python)
  • Re: Rich Comparisons Gotcha
    ... ValueError: The truth value of an array with more than one element is ... "I have come to believe that the whole world is an enigma, a harmless enigma ...
    (comp.lang.python)
  • Re: Rich Comparisons Gotcha
    ... ValueError: The truth value of an array with more than one element is ... Only in the sense that there are special cases where the array elements are all true, or all false, and numpy *could* safely return a bool. ...
    (comp.lang.python)
  • arrays in lists
    ... I have a list of arrays and want to find an array with list.index. ... ValueError: The truth value of an array with more than one element is ... from numpy import array ...
    (comp.lang.python)
  • Re: Bad Math
    ... > < 37, to arbitrary precision. ... > The function returns a string representing numerator/denominator in base ... > except ValueError: ... > Traceback: ...
    (comp.lang.python)