L=[] L[0]=2 L[1]=3
报错:IndexError: list assignment index out of range,列表超过限制
一种情况是:list[index]的index超出范围
另一种情况是:list是一个空的,没有一个元素,进行list[0]就会出现错误!
本例是第二种情况——声明了一个List对象,想通过List[index]=value的方式向其中添加元素
解决方法:
①用append的方法向其中添加元素
L.append(2)
L.append(3)
②不用List,改用Dict类型
很简单,把[]换成{}即可
L=[] => L={}
不过这时候L就不是List了,而是一个Dict字典对象