[英]IndexError: list index out of range - throwing error in Python?
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for i in a:
if a[i] % 2 == 0 :
l.append(a[i])
The code above keeps throwing the error - "IndexError: list index out of range"
上面的代码不断抛出错误-
"IndexError: list index out of range"
I cannot understand what to do ? 我不知道该怎么办?
When you perform for i in a
you are iterating over the elements of a
, not the indices! 当您
for i in a
表演时for i in a
您正在遍历a
的元素 ,而不是索引!
You are trying to access: a[1]
and then a[4]
and then a[9]
and then a[16]
->
Which is the one that is causing the IndexError
. 您正在尝试访问:
a[1]
,然后a[4]
,然后a[9]
,然后a[16]
->
这是引起IndexError
那个。
If you want to iterate only over the indices try: 如果只想遍历索引,请尝试:
>>> for i in range(len(a)):
if a[i] % 2 == 0 :
l.append(a[i])
>>> print (l)
[4, 16, 36, 64, 100]
If you need both the value and index use for index, value in enumerate(a):
. 如果同时需要索引的值和索引
for index, value in enumerate(a):
使用for index, value in enumerate(a):
。
When you are iterating over a
, you are looking at the value rather than the position of that value in the list. 当你遍历
a
,你正在寻找的价值,而不是在列表中值的位置。
You could use just the value like so: 您可以像这样使用值:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for val in a:
if val % 2 == 0 :
l.append(val)
Or alternately, if you need both the position and the value then you can use the enumerate
function, like so: 或者,如果您同时需要位置和值,则可以使用
enumerate
函数,如下所示:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for pos, val in enumerate(a):
if a[pos] % 2 == 0 :
l.append(a[pos])
There are some ways: 有一些方法:
for e in a:
if e % 2 == 0 :
l.append(e)
or 要么
for i in range(len(a)):
if a[i] % 2 == 0 :
l.append(a[i])
or 要么
for i, e in enumerate(a):
if e % 2 == 0 :
l.append(e)
or 要么
for i, e in enumerate(a):
if a[i] % 2 == 0 :
l.append(a[i])
You could use list comprehension for that: 您可以为此使用列表理解 :
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l = [i for i in a if i%2 == 0]
print(l)
[4, 16, 36, 64, 100]
列表组合可用于使其单线l=[x for x in a if x%2==0]
并且如ather答案所述,您的问题是您使用列表项作为其索引。
You can use filter for your task: 您可以对任务使用过滤器 :
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print filter(lambda x: x % 2 == 0, a)
as in other answers, i
takes the values from a
, which are [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
就像在其他答案中一样,
i
从a
取值[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
in the loop when i
takes the value " 16
", a[i]
will be out of range!! 在循环中,当
i
取值“ 16
”时, a[i]
将超出范围! ( 16 > len(a)
) (
16 > len(a)
)
for debugging i always suggest printing... 为了调试我总是建议打印...
in this case if you print the value of i
in the loop, you will find the problem yourself 在这种情况下,如果您在循环中打印
i
的值,您会自己发现问题
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for i in a:
if a[i] % 2 == 0 :
l.append(a[i])
...
if a[1] % 2 == 0:
...
if a[4] % 2 == 0:
...
if a[9] % 2 == 0:
...
if a[16] % 2 == 0:
index error, because the biggest index is 9 in array a... so u have to use this: 索引错误,因为最大索引是数组a中的9。因此,您必须使用以下代码:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for i in a:
if i % 2 == 0 :
l.append(i)
or this example: 或这个例子:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for i in a:
if i % 2 == 0 :
l.append(i)
or the 1 line solution: 或1行解决方案:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= [x for x in a if x%2 == 0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.