![](/img/trans.png)
[英]How do I remove the lowest n numbers in a numpy array and keep track of its index?
[英]How to keep track of the index of a numpy array element
我正在尝试使用for循环遍历numpy数组的项目。 有没有办法跟踪迭代当前项的索引(除了在循环之前初始化计数器并在循环内部递增计数器)?
myArray = [4 5 6 7]
for item in myArray:
print(index of item)
我想得到的是(请记住,这是一个numpy数组,而不是列表)
0
1
2
3
您可以像这样迭代:
import numpy as np
a = np.array([6,5,7,0,1,3,4])
for index in range(a.shape[0]): # use range with nparray.shape[0] to get the size
print(index) # you can do a[index] to get the value
这输出:
0
1
2
3
4
5
6
正确的方法是使用enumerate
:
myArray = [4, 5, 6, 7]
for i,item in enumerate(myArray):
print(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.