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
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@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
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@ucsd.edu "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter
participants (3)
-
Darren Dale
-
Michiel Jan Laurens de Hoon
-
Robert Kern