[Numpy-discussion] searching a list of arrays
Michiel Jan Laurens de Hoon
mdehoon at ims.u-tokyo.ac.jp
Sun Mar 27 19:01:13 CST 2005
This is because of how "==" is defined for arrays. For lists, list1==list2 if
all elements are the same; a boolean value is returned:
>>> x = [0,1,0]
>>> x==[0,1,0]
True
>>> x==[1,0,0]
False
For arrays, "==" does a element-wise comparison:
>>> from Numeric import *
>>> x = array([0,1,0])
>>> x==array([0,1,0])
array([1, 1, 1])
>>> x==array([1,0,0])
array([0, 0, 1])
>>>
Now, when you count how often array([0,1,0]) appears in b, actually you evaluate
element==array([0,1,0]) for each element in b, and count how often you get a
True, with every array other than array([0,0,0]) regarded as True.
For list a, this happens to work because array([0,1]) and array([1,0]) have no
elements in common. But in this case:
>>> a = [array([0,0]),array([0,0]),array([0,1]),array([0,1])]
>>> a
[array([0, 0]), array([0, 0]), array([0, 1]), array([0, 1])]
>>> a.count(array([0,0]))
4
you also get the non-intuitive answer 4.
An easy way to get this to work is to use lists instead of arrays:
>>> b = [[0,1,0], [0,1,0], [1,0,0], [1,0,0]]
>>> b.count([0,1,0])
2
But I don't know if this solution is suitable for your application.
--Michiel.
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.
>
> Does anyone know why 1x2 arrays work, but 1x3 or larger arrays do not?
>
> Thanks,
> Darren
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
>
--
Michiel de Hoon, Assistant Professor
University of Tokyo, Institute of Medical Science
Human Genome Center
4-6-1 Shirokane-dai, Minato-ku
Tokyo 108-8639
Japan
http://bonsai.ims.u-tokyo.ac.jp/~mdehoon
More information about the Numpy-discussion
mailing list