Iterating over the cells of an array?

Fernando Pérez fperez528 at yahoo.com
Sun May 12 14:46:18 EDT 2002


<posted & mailed>

Alex Martelli wrote:

> Fernando Pérez wrote:
>         ...
>> ravel is I believe a copy operation, much more expensive than a call to
> 
> Chacking isn't all that expensive, is it?
> 
>>>> x=Numeric.array([range(n,n+4) for n in range(3)])
>>>> x
> array([[0, 1, 2, 3],
>        [1, 2, 3, 4],
>        [2, 3, 4, 5]])
>>>> f=Numeric.ravel(x)
>>>> f
> array([0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5])
>>>> x[1,1]=111
>>>> x
> array([[  0,   1,   2,   3],
>        [  1, 111,   3,   4],
>        [  2,   3,   4,   5]])
>>>> f
> array([  0,   1,   2,   3,   1, 111,   3,   4,   2,   3,   4,   5])

Thanks! I guess I'd been putting an unnecessary extra if in all my code up to 
this point. It looks like ravel does 'the right thing': .flat if possible, 
and if not it returns a contiguous copy.

Cheers,

f



More information about the Python-list mailing list