NumPy教程-NumPy 数组迭代
NumPy教程-NumPy 数组迭代
gh_1d7504e4dee1
回复:python,领取Python面试题。分享Python教程,Python架构师教程,Python爬虫,Python编程视频,Python脚本,Pycharm教程,Python微服务架构,Python分布式架构,Pycharm注册码。
NumPy 提供了一个迭代器对象,即 nditer,可以使用 Python 标准的迭代器接口来迭代给定的数组。
考虑以下示例。
示例
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)
print(
"
Iterating over the transposed array:"
)
for
x
in
np.nditer(at):
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
迭代顺序
正如我们所知,有两种方式将值存储到 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(
"
Printing the array:
"
)
print(a)
print(
"
Printing the transpose of the array:
"
)
at = a.T
print(at)
print(
"
Iterating over the transposed array
"
)
for
x
in
np.nditer(at):
print(x, end=
’ ’
)
print(
"
Sorting the transposed array in C-style:
"
)
print(
"
Iterating over the C-style array:
"
)
for
x
in
np.nditer(at, order=
’C’
):
print(x, end=
’ ’
)
d = at.copy(order=
’F’
)
print(d)
print(
"Iterating over the F-style array:
"
)
for
x
in
np.nditer(d):
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
Iterating
over the F-style array:
1
2 3 4 2 4 5 6 10 20 39 3
我们可以在定义迭代器对象本身时指定顺序 ’C’ 或 ’F’。考虑以下示例。
示例
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)
print(
"
Iterating over the transposed array
"
)
for
x
in
np.nditer(at):
print(x, end=
’ ’
)
print(
"
Sorting the transposed array in C-style:
"
)
print(
"
Iterating over the C-style array:
"
)
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
数组值的修改
在迭代过程中,我们不能修改数组元素,因为与迭代器对象关联的 op-flag 设置为 readonly。
然而,我们可以将此标志设置为 readwrite 或 write only,以修改数组值。考虑以下示例。
示例
import
numpy
as
np
a = np.array([[
1
,
2
,
3
,
4
], [
2
,
4
,
5
,
6
], [
10
,
20
,
39
,
3
]])
print(
"
Printing the original array:
"
)
print(a)
print(
"
Iterating over the modified array
"
)
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
-
某大厂开始降本,抽纸从维达变成杂牌!
-
NumPy教程-NumPy 广播
-
一边是计算机就业哀鸿遍野 一边是高考生疯狂涌向计算机专业。。。
-
2023年血糖新标准公布,不是3.9-6.1,快来看看你的血糖正常吗? 2023-02-07
-
2023年各省最新电价一览!8省中午执行谷段电价! 2023-01-03
-
GB 55009-2021《燃气工程项目规范》(含条文说明),2022年1月1日起实施 2021-11-07
-
PPT导出高分辨率图片的四种方法 2022-09-22
-
2023年最新!国家电网27家省级电力公司负责人大盘点 2023-03-14
-
全国消防救援总队主官及简历(2023.2) 2023-02-10
-
盘点 l 中国石油大庆油田现任领导班子 2023-02-28
-
我们的前辈!历届全国工程勘察设计大师完整名单! 2022-11-18
-
关于某送变电公司“4·22”人身死亡事故的快报 2022-04-26
