# [Numpy-discussion] searching a list of arrays

Robert Kern rkern at ucsd.edu
Tue Mar 29 03:46:19 EST 2005

```Darren Dale wrote:
> Hi,
>
> I have a list of numeric-23.8 arrays:
>
> a = [array([0,1]),
>       array([0,1]),
>       array([1,0]),
>       array([1,0])]
>
> b = [array([0,1,0]),
>       array([0,1,0]),
>       array([1,0,0]),
>       array([1,0,0])]
>
> and I want to make a new list out of b:
>
> c = [array([0,1,2]),
>       array([1,0,2])]
>
> where the last index in each array is the result of
>
> b.count([0,1,0]) # or [1,0,0]
>
>
> The problem is that the result of b.count(array([1,0,0])) is 4, not 2, and
> b.remove(array([1,0,0])) indescriminantly removes arrays from the list.
> a.count and a.remove work the way I expected.

This is a result of rich comparisons. (array1 == array2) yields an
array, not a boolean.

In [1]:a = [array([0,1]),
...:       array([0,1]),
...:             array([1,0]),
...:                   array([1,0])]

In [2]:b = [array([0,1,0]),
...:       array([0,1,0]),
...:             array([1,0,0]),
...:                   array([1,0,0])]

In [3]:

In [3]:b.count(array([0,1,0]))
Out[3]:4

In [4]:[x == array([0,1,0]) for x in b]
Out[4]:
[array([1, 1, 1],'b'),
array([1, 1, 1],'b'),
array([0, 0, 1],'b'),
array([0, 0, 1],'b')]

To replace b.count(), you can do

In [12]:sum(alltrue(equal(b, array([0,1,0])), axis=-1))
Out[12]:2

To replace b.remove(), you can do

In [14]:[x for x in b if not alltrue(x == array([0,1,0]))]
Out[14]:[array([1, 0, 0]), array([1, 0, 0])]

--
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

```