在 Python 3 中迭代数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51919448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterating over arrays in Python 3
提问by q-compute
I haven't been coding for awhile and trying to get back into Python. I'm trying to write a simple program that sums an array by adding each array element value to a sum. This is what I have:
我有一段时间没有编码并试图重新使用 Python。我正在尝试编写一个简单的程序,通过将每个数组元素值添加到总和来对数组求和。这就是我所拥有的:
def sumAnArray(ar):
theSum = 0
for i in ar:
theSum = theSum + ar[i]
print(theSum)
return theSum
I get the following error:
我收到以下错误:
line 13, theSum = theSum + ar[i]
IndexError: list index out of range
I found that what I'm trying to do is apparently as simple as this:
我发现我想要做的事情显然就像这样简单:
sum(ar)
But clearly I'm not iterating through the array properly anyway, and I figure it's something I will need to learn properly for other purposes. Thanks!
但很明显,无论如何我都没有正确地遍历数组,我认为这是我需要为其他目的正确学习的东西。谢谢!
回答by Mr Alihoseiny
When you loop in an array like you did, your for variable(in this example i
) is current element of your array.
当你像你一样在数组中循环时,你的 for 变量(在这个例子中i
)是数组的当前元素。
For example if your ar
is [1,5,10]
, the i
value in each iteration is 1
, 5
, and 10
.
And because your array length is 3, the maximum index you can use is 2. so when i = 5
you get IndexError
.
You should change your code into something like this:
例如,如果你的ar
就是[1,5,10]
,将i
在每次迭代中值是1
,5
和10
。因为你的数组长度是 3,所以你可以使用的最大索引是 2。所以当i = 5
你得到IndexError
. 你应该把你的代码改成这样:
for i in ar:
theSum = theSum + i
Or if you want to use indexes, you should create a range from 0 ro array length - 1
.
或者,如果您想使用索引,您应该从 0 ro 创建一个范围array length - 1
。
for i in range(len(ar)):
theSum = theSum + ar[i]
回答by umluizlima
The for loop iterates over the elements of the array, not its indexes. Suppose you have a list ar = [2, 4, 6]:
for 循环遍历数组的元素,而不是它的索引。假设您有一个列表 ar = [2, 4, 6]:
When you iterate over it with for i in ar:
the values of i will be 2, 4 and 6. So, when you try to access ar[i]
for the first value, it might work (as the last position of the list is 2, a[2] equals 6), but not for the latter values, as a[4] does not exist.
当您使用for i in ar:
i 的值对其进行迭代时, i 将是 2、4 和 6。因此,当您尝试访问ar[i]
第一个值时,它可能会起作用(因为列表的最后一个位置是 2,a[2] 等于 6 ),但不适用于后面的值,因为 a[4] 不存在。
If you intend to use indexes anyhow, try using for index, value in enumerate(ar):
, then theSum = theSum + ar[index]
should work just fine.
如果您打算无论如何使用索引,请尝试使用for index, value in enumerate(ar):
,然后theSum = theSum + ar[index]
应该可以正常工作。
回答by ajay mathur
You can use
您可以使用
nditer
Here I calculated no. of positive and negative coefficients in a logistic regression:
在这里我计算了没有。逻辑回归中的正负系数:
b=sentiment_model.coef_
pos_coef=0
neg_coef=0
for i in np.nditer(b):
if i>0:
pos_coef=pos_coef+1
else:
neg_coef=neg_coef+1
print("no. of positive coefficients is : {}".format(pos_coef))
print("no. of negative coefficients is : {}".format(neg_coef))
Output:
输出:
no. of positive coefficients is : 85035
no. of negative coefficients is : 36199
回答by Faizi
While iterating over a list or array with this method:
使用此方法迭代列表或数组时:
ar = [10, 11, 12]
for i in ar:
theSum = theSum + ar[i]
You are actually getting the values of list or array sequentially in i
variable.
If you print the variable i
inside the for loop
. You will get following output:
您实际上是在i
变量中按顺序获取列表或数组的值。如果i
在for loop
. 您将获得以下输出:
10
11
12
However, in your code you are confusing i
variable with index value of array. Therefore, while doing ar[i]
will mean ar[10]
for the first iteration. Which is of course index out of range throwing IndexError
但是,在您的代码中,您将i
变量与数组的索引值混淆了。因此,while doar[i]
将意味着ar[10]
进行第一次迭代。这当然是索引超出范围抛出IndexError
EditYou can read thisfor better understanding of different methods of iterating over array or list in Python
编辑您可以阅读本文以更好地理解在 Python 中迭代数组或列表的不同方法