469,927 Members | 1,890 Online

# iterator over a numarray?

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
Jul 18 '05 #1
3 1594
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....

I'd suggest you repost this (and you other numarray question) directly to the
numarray list (numpy-dicsussion). The response rate tends to be better for
numpy/numarray specific questions on that list, since the users of those libs
don't always follow c.l.py regularly (though most follow it on and off).

Best,

f
Jul 18 '05 #2
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.

Jul 18 '05 #3
Colin J. Williams wrote:
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.

After a little hunting I found something that did the trick via google
groups.
The article in question is here:

it details a way to generate a list of coordinate and value pairs, e.g.
values from a 2d numarray would then be represented as:

(((0,0), 2), ((1,0), 65), ((2,0), 33), ....etc)

thanks!
alex
Jul 18 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion.