【发布时间】:2017-08-06 22:50:46
【问题描述】:
我正在尝试修改临时列表并将临时列表存储在可能的列表中,但我需要让 list1 保持不变。当我通过 Python 运行它时,我的临时列表没有改变,所以我想知道哪里出了问题。
list1 = [['1', '1', '1'],
['0', '0', '0'],
['2', '2', '2']]
temp = list1
possible = []
for i in range(len(temp)-1):
if(temp[i][0] == 1):
if(temp[i+1][0] == 0):
temp[i+1][0] == 1
possible = possible + temp
temp = list1
print(possible)
【问题讨论】:
-
temp = list1不是副本。您根本没有创建临时列表。见nedbatchelder.com/text/names.html -
使用
=,而不是==。