enumerate 遍历numpy数组
觉得有用的话,欢迎一起讨论相互学习~
遍历一维数组
import numpy as np
a=np.array([1,2,3,4])
for i,j in enumerate(a):
print("i",i,"j",j)
# i 0 j 1
# i 1 j 2
# i 2 j 3
# i 3 j 4
遍历二维数组
- i,j 分别表示数组的 行的索引 和 每一行中存储的值
import numpy as np
for i,j in enumerate(b):
print("i",i,"j",j)
# i 0 j 1
# i 1 j 2
# i 2 j 3
# i 3 j 4
# i 0 j [1 2 3 4]
# i 1 j [5 6 7 8]
# i 2 j [ 9 10 11 12]
遍历二维数组集合
for i,(xs,ys) in enumerate(zip(a,b)):
print("i",i,"(xs,ys)",xs,ys)
# i 0 (xs,ys) 1 [1 2 3 4]
# i 1 (xs,ys) 2 [5 6 7 8]
# i 2 (xs,ys) 3 [ 9 10 11 12]