我有以下MWE:
def get_files():
file_list = ['first', 'second', 'third', 'fourth']
return file_list
def set_names(orig_flist):
file_list = []
for i in range(len(orig_flist)):
file_list[i] = orig_flist[i]
return file_list
set_names(get_files())
当我运行它时,我收到此错误:
Traceback (most recent call last):
File "privpub.py", line 11, in <module>
set_names(get_files())
File "privpub.py", line 8, in set_names
file_list[i] = orig_flist[i]
IndexError: list assignment index out of range
我不明白发生了什么。有人可以解释一下吗?
答案 0 :(得分:3)
您正尝试为列表中尚不存在的索引分配值:
Login Activity
您需要使用file_list = []
for i in range(len(orig_flist)):
file_list[i] = orig_flist[i]
来延长列表,如下所示:
append