iterator over a numarray?
Colin J. Williams
cjw at sympatico.ca
Mon Jul 5 11:50:25 EDT 2004
Alex Hunsley wrote:
> I'm looking for a way to iterate over all the items in a numarray.
> Writing a few nested loops isn't going to cut it, because the numarray
> in question could be of any dimension...
> I am aware of the revel function, but that appears to just flatten the
> numarray. What I need is an iterator that can give each value and the
> coordinates in the array of that item....
>
> thanks
> alex
Alex,
You might try something like:
import numarray as _num
a= _num.arange(48, shape= (4, 4, 3))
b= iter()
c= b.next()
print `c`
d= b.next()
print `d`
This gives:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
array([[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]])
You can see that we move along the first dimension.
ravel() flattens an array in situ, flat delivers a flattened array.
Colin W.
More information about the Python-list
mailing list