list assignment index out of range

现象

m1=[]
for i in xrange(n):
m1[i]=1

报错:IndexError: list assignment index out of range

分析

空数组不能直接指定位置

解决方法1

m1.append(1)

解决方法2

先生成一个定长的list:

m1=[0]*len(data)

m1[1]=1

猜你喜欢

转载自www.cnblogs.com/4c4853/p/9785116.html