list=[]
i =0
list[i] =1
Traceback (most recent call last):
File "D:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-28c9e44edb79>", line 1, in <module>
list[i] =1
IndexError: list assignment index out of range
list[0]=1
Traceback (most recent call last):
File "D:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-692d9d6c3702>", line 1, in <module>
list[0]=1
IndexError: list assignment index out of range
list1=[]
print(list1)
[]
for i,x in enumerate(list1):
print(i,x) list1.append(1)
print(list1)
[1]
即:空的list不能直接用角标赋值,会报错
要用append进行填充
或者下面这样:
list1=[]
list2=[1,2,3]
list1=list2
list1
Out[17]: [1, 2, 3]