索引错误:列表分配索引超出范围。

时间:2021-02-11 16:40:18

Please consider the following code:

请考虑以下代码:

i = [1, 2, 3, 5, 8, 13]
j = []
k = 0

for l in i:
    j[k] = l
    k += 1

print j

The output (Python 2.6.6 on Win 7 32-bit) is:

输出(Python 2.6.6 on Win 7 32位)是:

> Traceback (most recent call last): 
>     j[k] = l IndexError: list assignment index out of range

I guess it's something simple I don't understand. Can someone clear it up?

我想这很简单,我不明白。有人能清理一下吗?

8 个解决方案

#1


192  

j is an empty list, but you're attempting to write to element [0] in the first iteration, which doesn't exist yet.

j是一个空的列表,但是在第一次迭代中,您试图写入元素[0],这还不存在。

Try the following instead, to add a new element to the end of the list:

尝试下面的方法,在列表的末尾添加一个新元素:

for l in i:
    j.append(l)

#2


28  

Your other option is to initialize j:

另一个选项是初始化j:

j = [None]*max(i)

#3


13  

Do j.append(l) instead of j[k] = l and avoid k at all.

j。append(l)代替j[k] = l,并避免k。

#4


9  

For the example you posted, you could also use a list comprehension:

对于您发布的示例,您还可以使用列表理解:

j = [l for l in i]

or just make a copy:

或者只是做一个拷贝:

j = i[:]

#5


6  

j.append(l)

Also avoid using lower-case "L's" because it is easy for them to be confused with 1's

也要避免使用小写的“L”,因为它们容易被混淆为1。

#6


3  

I think python method insert what are you looking for;

我认为python方法插入了你想要的东西;

Inserts element x at position i.

插入元素x在位置i。

array = [1,2,3,4,5]

array.insert(1,2)

print array

# prints [2,2,3,4,5]

#7


3  

You could use a dictionary (similar to an associative array) for j

您可以使用字典(类似于关联数组)作为j。

i = [1, 2, 3, 5, 8, 13]
j = {} #initiate as dictionary
k = 0

for l in i:
    j[k] = l
    k += 1

print j

will print :

将打印:

{0: 1, 1: 2, 2: 3, 3: 5, 4: 8, 5: 13}

#8


1  

One more way:

一个方法:

j=i[0]
for k in range(1,len(i)):
    j = numpy.vstack([j,i[k]])

In this case j will be a numpy array

在这种情况下,j将是一个numpy数组。

#1


192  

j is an empty list, but you're attempting to write to element [0] in the first iteration, which doesn't exist yet.

j是一个空的列表,但是在第一次迭代中,您试图写入元素[0],这还不存在。

Try the following instead, to add a new element to the end of the list:

尝试下面的方法,在列表的末尾添加一个新元素:

for l in i:
    j.append(l)

#2


28  

Your other option is to initialize j:

另一个选项是初始化j:

j = [None]*max(i)

#3


13  

Do j.append(l) instead of j[k] = l and avoid k at all.

j。append(l)代替j[k] = l,并避免k。

#4


9  

For the example you posted, you could also use a list comprehension:

对于您发布的示例,您还可以使用列表理解:

j = [l for l in i]

or just make a copy:

或者只是做一个拷贝:

j = i[:]

#5


6  

j.append(l)

Also avoid using lower-case "L's" because it is easy for them to be confused with 1's

也要避免使用小写的“L”,因为它们容易被混淆为1。

#6


3  

I think python method insert what are you looking for;

我认为python方法插入了你想要的东西;

Inserts element x at position i.

插入元素x在位置i。

array = [1,2,3,4,5]

array.insert(1,2)

print array

# prints [2,2,3,4,5]

#7


3  

You could use a dictionary (similar to an associative array) for j

您可以使用字典(类似于关联数组)作为j。

i = [1, 2, 3, 5, 8, 13]
j = {} #initiate as dictionary
k = 0

for l in i:
    j[k] = l
    k += 1

print j

will print :

将打印:

{0: 1, 1: 2, 2: 3, 3: 5, 4: 8, 5: 13}

#8


1  

One more way:

一个方法:

j=i[0]
for k in range(1,len(i)):
    j = numpy.vstack([j,i[k]])

In this case j will be a numpy array

在这种情况下,j将是一个numpy数组。