Comparision of numpy module array with list
Question: I am trying to compare numpy array with list.... Because i am new to python, i
don't know about numpy array I want to know the application of numpy array
Please help out in understanding numpy array.

    
    
    >>> from numpy import *
    >>> res1 = []
    >>> res2 = array([])
    >>> if res1 == res2:
    ...    print 'hi'
    ... else:
    ...    print 'bye'
    ... 
    bye
    
    >>> res1 = [1]
    >>> res2 = array([1])
    >>> if res1 == res2:
    ...    print 'hi'
    ... else:
    ...    print 'bye'
    ... 
    hi
    >>> res1 = [1,2]
    >>> res2 = array([1, 2])
    >>> if res1 == res2:
    ...    print 'hi'
    ... else:
    ...    print 'bye'
    ... 
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: The truth value of an array with more than one element is ambiguous. Use    a.any() or a.all()
    

Any one please help why i am getting value error


Answer: The comparison `res1 == res2` creates a new array of booleans, which represent
an element-wise comparison of the list and array contents:

    
    
    >>> res1 == res2
    array([ True,  True], dtype=bool)
    

As the error message tells you, you cannot directly assign a truth value to an
array, so you need to test whether `all` items are `True`:

    
    
    >>> np.all(res1 == res2)
    True
    

This only happens because your list and array are the same shape:

    
    
    >>> a = np.array([1, 2, 3])
    >>> b = [3, 2]
    >>> a == b
    False
    

Note that I have used `import numpy as np` rather than `from numpy import *`
\- this means that I don't override e.g. the built-in `all` with `numpy`'s
version.

