Modifying a list within a list in Python
本问题已经有最佳答案,请猛点这里访问。
我正试图修改临时列表并将临时列表存储在可能的列表中,但我需要保持列表1不变。当我在python中运行这个命令时,我的临时列表没有改变,所以我想知道这个错误在哪里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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) |
由于
数组以
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from copy import deepcopy list1 = [['1', '1', '1'], ['0', '0', '0'], ['2', '2', '2']] # copying element from list1 temp = deepcopy(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 print('Contents of possible: ', possible) print('Contents of list1: ', list1) print('Contents of temp: ', temp) |