Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy Iterating Arrays

NumPy Iterating Arrays

NumPy's iterator object numpy.nditer provides a flexible way to access the elements of one or more arrays.

The most basic task of an iterator is to accomplish access to array elements.

Next, we use the arange() function to create a 2X3 array and iterate over it with nditer.

Example

import

numpy

as

np

a

=

np

.

arange

(

6

)

.

reshape

(

2

,

3

)

print

(

'

The original array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Iterate and output elements:

'

)

for

x

in

np

.

nditer

(

a

)

:

print

(

x

,

end

=

"

,

"

)

print

(

'

\n

'

)

The output is:

原始数组是:

[[0 1 2]

 [3 4 5]]





Iterate and output elements:

0, 1, 2, 3, 4, 5, 

The above example does not use standard C or Fortran order; the chosen order is consistent with the array's memory layout to improve access efficiency, defaulting to row-major order (or C-order).

This reflects that by default only each element needs to be accessed, without regard to its specific order. We can see this by iterating over the transpose of the above array and comparing it with the copy approach of accessing the transposed array in C order, as in the following example:

Example

import

numpy

as

np

a

=

np

.

arange

(

6

)

.

reshape

(

2

,

3

)

for

x

in

np

.

nditer

(

a

.

T

)

:

print

(

x

,

end

=

"

,

"

)

print

(

'

\n

'

)

for

x

in

np

.

nditer

(

a

.

T

.

copy

(

order

=

'

C

'

)

)

:

print

(

x

,

end

=

"

,

"

)

print

(

'

\n

'

)

The output is:

0, 1, 2, 3, 4, 5, 



0, 3, 1, 4, 2, 5, 

From the above example, a and a.T are traversed in the same order, meaning their storage order in memory is also the same, but a.T.copy(order = 'C') gives a different traversal result because its storage layout differs from the former two; it is accessed by row by default.

Controlling traversal order

Example

import

numpy

as

np

a

=

np

.

arange

(

0

,

60

,

5

)

a

=

a

.

reshape

(

3

,

4

)

print

(

'

The original array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

The transpose of the original array is:

'

)

b

=

a

.

T

print

(

b

)

print

(

'

\n

'

)

print

(

'

Sorted in C-style order:

'

)

c

=

b

.

copy

(

order

=

'

C

'

)

print

(

c

)

for

x

in

np

.

nditer

(

c

)

:

print

(

x

,

end

=

"

,

"

)

print

(

'

\n

'

)

print

(

'

Sorted in F-style order:

'

)

c

=

b

.

copy

(

order

=

'

F

'

)

print

(

c

)

for

x

in

np

.

nditer

(

c

)

:

print

(

x

,

end

=

"

,

"

)

The output is:

原始数组是:

[[ 0  5 10 15]

 [20 25 30 35]

 [40 45 50 55]]





The transpose of the original array is:

[[ 0 20 40]

 [ 5 25 45]

 [10 30 50]

 [15 35 55]]





Sort in C style order:

[[ 0 20 40]

 [ 5 25 45]

 [10 30 50]

 [15 35 55]]

0, 20, 40, 5, 25, 45, 10, 30, 50, 15, 35, 55, 



Sort in F style order:

[[ 0 20 40]

 [ 5 25 45]

 [10 30 50]

 [15 35 55]]

0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55,

You can force the nditer object to use a certain order by setting it explicitly:

Example

import

numpy

as

np

a

=

np

.

arange

(

0

,

60

,

5

)

a

=

a

.

reshape

(

3

,

4

)

print

(

'

The original array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Sorted in C-style order:

'

)

for

x

in

np

.

nditer

(

a

,

order

=

'

C

'

)

:

print

(

x

,

end

=

"

,

"

)

print

(

'

\n

'

)

print

(

'

Sorted in F-style order:

'

)

for

x

in

np

.

nditer

(

a

,

order

=

'

F

'

)

:

print

(

x

,

end

=

"

,

"

)

The output is:

原始数组是:

[[ 0  5 10 15]

 [20 25 30 35]

 [40 45 50 55]]





Sort in C style order:

0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 



Sort in F style order:

0, 20, 40, 5, 25, 45, 10, 30, 50, 15, 35, 55,

Modifying element values in the array

The nditer object has another optional parameter, op_flags. By default, nditer treats the array to be iterated as a read-only object; to modify element values while iterating, you must specify readwrite or writeonly mode.

Example

import

numpy

as

np

a

=

np

.

arange

(

0

,

60

,

5

)

a

=

a

.

reshape

(

3

,

4

)

print

(

'

The original array is:

'

)

print

(

a

)

print

(

'

\n

'

)

for

x

in

np

.

nditer

(

a

,

op_flags

=

[

'

readwrite

'

]

)

:

x

[

...

]

=

2

x

print

(

'

The modified array is:

'

)

print

(

a

)

The output is:

原始数组是:

[[ 0  5 10 15]

 [20 25 30 35]

 [40 45 50 55]]





The modified array is:

[[  0  10  20  30]

 [ 40  50  60  70]

 [ 80  90 100 110]]

Using an outer loop

The constructor of the nditer class has a flags parameter, which can accept the following values:

Parameters Description
c_index Can track indices in C order
f_index Can track indices in Fortran order
multi_index Can track one index type per iteration
external_loop The value given is a one-dimensional array with multiple values, rather than a zero-dimensional array

In the following example, the iterator iterates over each column and combines them into a one-dimensional array.

Example

import

numpy

as

np

a

=

np

.

arange

(

0

,

60

,

5

)

a

=

a

.

reshape

(

3

,

4

)

print

(

'

The original array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

The modified array is:

'

)

for

x

in

np

.

nditer

(

a

,

flags

=

[

'

external_loop

'

]

,

order

=

'

F

'

)

:

print

(

x

,

end

=

"

,

"

)

The output is:

原始数组是:

[[ 0  5 10 15]

 [20 25 30 35]

 [40 45 50 55]]





The modified array is:

[ 0 20 40], [ 5 25 45], [10 30 50], [15 35 55],

Broadcasting iteration

If two arrays are broadcastable, the nditer combined object can iterate over them simultaneously. Suppose array a has dimensions 3X4 and array b has dimensions 1X4; then the following iterator is used (array b is broadcast to the size of a).

Example

import

numpy

as

np

a

=

np

.

arange

(

0

,

60

,

5

)

a

=

a

.

reshape

(

3

,

4

)

print

(

'

The first array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

The second array is:

'

)

b

=

np

.

array

(

[

1

,

2

,

3

,

4

]

,

dtype

=

int

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

The modified array is:

'

)

for

x

,

y

in

np

.

nditer

(

[

a

,

b

]

)

:

print

(

"

%d:%d

"

%

(

x

,

y

)

,

end

=

"

,

"

)

The output is:

第一个数组为:

[[ 0  5 10 15]

 [20 25 30 35]

 [40 45 50 55]]





The second array is:

[1 2 3 4]





The modified array is:

0:1, 5:2, 10:3, 15:4, 20:1, 25:2, 30:3, 35:4, 40:1, 45:2, 50:3, 55:4,