请考虑下面的代码:IndexError:列表分配索引超出范围
i = [1, 2, 3, 5, 8, 13]
j = []
k = 0
for l in i:
j[k] = l
k += 1
print j
输出(Python的2.6.6在Win 7 32位)是:
> Traceback (most recent call last):
> j[k] = l IndexError: list assignment index out of range
我想这是简单的东西我不明白。有人可以清除它吗?
'append'是为您的使用情况下,正确的解决方案。然而, python列表上有一个插入方法,它可以直接插入列表中的第i个位置。 'j.insert(k,l)' – opensourcegeek 2015-09-30 08:46:47