Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
索引器错误:列表索引超出范围-在Python中引发错误?_Python_List_Error Handling_Append_Indexoutofboundsexception - Fatal编程技术网

索引器错误:列表索引超出范围-在Python中引发错误?

索引器错误:列表索引超出范围-在Python中引发错误?,python,list,error-handling,append,indexoutofboundsexception,Python,List,Error Handling,Append,Indexoutofboundsexception,上面的代码不断抛出错误-索引器:列表索引超出范围“ 我不明白该怎么办?当您在a中为I执行时,您正在迭代a的元素,而不是索引 您正在尝试访问:a[1],然后是a[4],然后是a[9],然后是a[16]->,这是导致索引器的原因 如果只想在索引上迭代,请尝试: 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]) 如果同时需要值和索引,请使

上面的代码不断抛出错误-
索引器:列表索引超出范围“


我不明白该怎么办?

当您在a中为I执行
时,您正在迭代a
元素,而不是索引

您正在尝试访问:
a[1]
,然后是
a[4]
,然后是
a[9]
,然后是
a[16]
->
,这是导致
索引器的原因

如果只想在索引上迭代,请尝试:

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])

如果同时需要值和索引,请使用
作为索引,枚举(a)中的值:

当您迭代
a
时,您查看的是值,而不是该值在列表中的位置

您可以使用如下值:

>>> for i in range(len(a)):
        if a[i] % 2 == 0 :
            l.append(a[i])


>>> print (l)
[4, 16, 36, 64, 100]
或者,如果同时需要位置和值,则可以使用
枚举
函数,如下所示:

a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for val in a:
    if val % 2 == 0 :
        l.append(val)
有一些方法:

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])

您可以使用:

for i, e in enumerate(a):
  if a[i] % 2 == 0 :
    l.append(a[i])

列表压缩可用于使其成为一个衬里
l=[x代表x,如果x%2==0]
正如他们回答中所述,您的问题是您正在使用列表项作为其索引。

您可以在任务中使用筛选器:

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]

与其他答案一样,
i
a
中获取值,即
[1,4,9,16,25,36,49,64,81,100]

在循环中,当
i
取值“
16
”时,
a[i]
将超出范围!!(
16>镜头(a)

对于调试,我始终建议打印…
在这种情况下,如果您在循环中打印
i
的值,您自己就会发现问题

a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print filter(lambda x: x % 2 == 0, a)
索引错误,因为数组中的最大索引是9。。。 所以你必须用这个:

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:
或者这个例子:

a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for i in a:
    if i % 2 == 0 :
        l.append(i)
或单线解决方案:

a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for i in a:
    if i % 2 == 0 :
        l.append(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:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for i in a:
    if i % 2 == 0 :
        l.append(i)
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= []
for i in a:
    if i % 2 == 0 :
        l.append(i)
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
l= [x for x in a if x%2 == 0]