本文概述
NumPy提供了一个迭代器对象, 即nditer, 可以使用python标准的Iterator接口在给定数组上进行迭代。
考虑以下示例。
例子
import numpy as np
a = np.array([[1, 2, 3, 4], [2, 4, 5, 6], [10, 20, 39, 3]])
print("Printing array:")
print(a);
print("Iterating over the array:")
for x in np.nditer(a):
print(x, end=' ')
输出
Printing array:
[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]
Iterating over the array:
1 2 3 4 2 4 5 6 10 20 39 3
迭代顺序不遵循任何特殊的顺序, 例如行优先或列顺序。但是, 它旨在匹配阵列的内存布局。
让我们遍历上面示例中给出的数组的转置。
例子
import numpy as np
a = np.array([[1, 2, 3, 4], [2, 4, 5, 6], [10, 20, 39, 3]])
print("Printing the array:")
print(a)
print("Printing the transpose of the array:")
at = a.T
print(at)
#this will be same as previous
for x in np.nditer(at):
print(print("Iterating over the array:")
for x in np.nditer(a):
print(x, end=' ')
输出
Printing the array:
[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]
Printing the transpose of the array:
[[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]
1 2 3 4 2 4 5 6 10 20 39 3
迭代顺序
众所周知, 有两种方法将值存储到numpy数组中:
- F风格订单
- C风格订单
让我们看一下numpy迭代器如何处理特定订单(F或C)的示例。
例子
import numpy as np
a = np.array([[1, 2, 3, 4], [2, 4, 5, 6], [10, 20, 39, 3]])
print("\nPrinting the array:\n")
print(a)
print("\nPrinting the transpose of the array:\n")
at = a.T
print(at)
print("\nIterating over the transposed array\n")
for x in np.nditer(at):
print(x, end= ' ')
print("\nSorting the transposed array in C-style:\n")
c = at.copy(order = 'C')
print(c)
print("\nIterating over the C-style array:\n")
for x in np.nditer(c):
print(x, end=' ')
d = at.copy(order = 'F')
print(d)
print("Iterating over the F-style array:\n")
for x in np.nditer(d):
print(x, end=' ')
输出
Printing the array:
[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]
Printing the transpose of the array:
[[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]
Iterating over the transposed array
1 2 3 4 2 4 5 6 10 20 39 3
Sorting the transposed array in C-style:
[[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]
Iterating over the C-style array:
1 2 10 2 4 20 3 5 39 4 6 3 [[ 1 2 10]
[ 2 4 20]
[ 3 5 39]
[ 4 6 3]]
Iterating over the F-style array:
1 2 3 4 2 4 5 6 10 20 39 3
在定义Iterator对象本身时, 我们可以提及顺序” C”或” F”。考虑以下示例。
例子
import numpy as np
a = np.array([[1, 2, 3, 4], [2, 4, 5, 6], [10, 20, 39, 3]])
print("\nPrinting the array:\n")
print(a)
print("\nPrinting the transpose of the array:\n")
at = a.T
print(at)
print("\nIterating over the transposed array\n")
for x in np.nditer(at):
print(x, end= ' ')
print("\nSorting the transposed array in C-style:\n")
print("\nIterating over the C-style array:\n")
for x in np.nditer(at, order = 'C'):
print(x, end=' ')
输出
Iterating over the transposed array
1 2 3 4 2 4 5 6 10 20 39 3
Sorting the transposed array in C-style:
Iterating over the C-style array:
1 2 10 2 4 20 3 5 39 4 6 3
数组值修改
由于与Iterator对象关联的op-flag设置为readonly, 因此我们无法在迭代期间修改数组元素。
但是, 我们可以将此标志设置为读写或仅写以修改数组值。考虑以下示例。
例子
import numpy as np
a = np.array([[1, 2, 3, 4], [2, 4, 5, 6], [10, 20, 39, 3]])
print("\nPrinting the original array:\n")
print(a)
print("\nIterating over the modified array\n")
for x in np.nditer(a, op_flags = ['readwrite']):
x[...] = 3 * x;
print(x, end = ' ')
输出
Printing the original array:
[[ 1 2 3 4]
[ 2 4 5 6]
[10 20 39 3]]
Iterating over the modified array
3 6 9 12 6 12 15 18 30 60 117 9
评论前必须登录!
注册