在我的python代码中,我想在循环中创建一个列表列表...但是在完成循环之后我最终在所有元素中都使用相同的列表...是因为列表是指向的而不是存储?如果是这样,我怎样才能找到解决问题的方法呢?以下是我的代码
list_lists=list()
list_temp=list()
for i in xrange(n):
ind_count =0
del list_temp[0:len(list_temp)]
for j in xrange(no_words):
if inp[i] == words[j]:
list_temp.append(j)
list_lists.append(list_temp)
Run Code Online (Sandbox Code Playgroud)
而不是删除列表中的项目(del list_temp[0:len(list_temp)])只需指定一个新列表:list_temp = list().您也可以使用速记:list_temp = []
您面临的问题是您总是重复使用在第二行中创建的相同列表:list_temp = list().您只是一遍又一遍地插入对这个列表的引用.
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |