From: George N. <gn...@go...> - 2006-05-18 12:16:59
|
This is a very useful thread. Made me think about what the present arrangement is supposed to do. My usage of this is to get out indices corresponding to a given condition. So E.g. In [38]: a = arange(12).reshape(3,2,2) Out[39]: array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]]]) In [40]: xx = where(a%2==0) In [41]: xx Out[41]: (array([0, 0, 1, 1, 2, 2]), array([0, 1, 0, 1, 0, 1]), array([0, 0, 0, 0, 0, 0])) The transpose idea should have been obvious to me... In [48]: for i,j,k in transpose(xx): ....: print i,j,k ....: ....: 0 0 0 0 1 0 1 0 0 1 1 0 2 0 0 2 1 0 A 1D array In [57]: aa = a.ravel() In [49]: xx = where(aa%2==0) In [53]: xx Out[53]: (array([ 0, 2, 4, 6, 8, 10]),) needs tuple unpacking: n [52]: for i, in transpose(xx): ....: print i ....: ....: 0 2 4 6 8 10 Or more simply, the ugly In [58]: for i in xx[0]: ....: print i ....: ....: 0 2 4 6 8 10 Alternatively, instead of transpose, we can simply use zip. E.g. (3D) In [42]: for i,j,k in zip(*xx): ....: print i,j,k or (1D, again still needs unpacking) In [56]: for i, in zip(*xx): ....: print i |