![](https://secure.gravatar.com/avatar/6bf7afbdd095bea91434590431817805.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/cd2cc75eb2c8ecc48758f87f8e6e53c4.jpg?s=120&d=mm&r=g)
This is because of how "==" is defined for arrays. For lists, list1==list2 if all elements are the same; a boolean value is returned:
For arrays, "==" does a element-wise comparison:
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:
you also get the non-intuitive answer 4. An easy way to get this to work is to use lists instead of arrays:
But I don't know if this solution is suitable for your application. --Michiel. Darren Dale wrote:
-- 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
![](https://secure.gravatar.com/avatar/ec366db3649cf13f4061b519193849d6.jpg?s=120&d=mm&r=g)
Darren Dale wrote:
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
![](https://secure.gravatar.com/avatar/cd2cc75eb2c8ecc48758f87f8e6e53c4.jpg?s=120&d=mm&r=g)
This is because of how "==" is defined for arrays. For lists, list1==list2 if all elements are the same; a boolean value is returned:
For arrays, "==" does a element-wise comparison:
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:
you also get the non-intuitive answer 4. An easy way to get this to work is to use lists instead of arrays:
But I don't know if this solution is suitable for your application. --Michiel. Darren Dale wrote:
-- 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
![](https://secure.gravatar.com/avatar/ec366db3649cf13f4061b519193849d6.jpg?s=120&d=mm&r=g)
Darren Dale wrote:
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