【问题标题】:List is suddenly changed, eventhough i dont change it列表突然变了,虽然我没变
【发布时间】:2018-04-11 10:44:29
【问题描述】:

我有相当复杂的代码。共有三个列表。简而言之 - 应该比较 list1 和比较列表,如果发现某些特定匹配,我们将 list1 中的值添加到 list2。整个代码如下

list1 = [['item1', ['item2'], '0', '0'], ['item3', ['item4'], '107', '2'], ['item4.5', ['item5', 'item4.5 aaa'], '120', '2'], ['item6', ['item6 item6 aaa'], '127', '1'], ['item7', ['item7 item7 aaa'], '129', '1']]
comparsion_list = [['item1', ['item2'], 'unknown'], ['item3', ['item4'], 'unknown'], ['item4.5', ['item5', 'item4.5 aaa'], 'unknown'], ['item6', ['item6 item6 aaa'], 'unknown']]
list2 = [['category', ['keywords'], ['long-names'], 'amount', 'amount2'],['empty', ['empty'], ['empty'], 'empty', 'empty']]

for a in range(len(comparsion_list)): #we go trough comparsion_list -start number is 1, end is category len
    for i in range(len(list1)): #and compare them with each item of list1

        if list1[i][1][0] in comparsion_list[a][1] and comparsion_list[a][2] not in [x[0] for x in list2]:
            list2.append([comparsion_list[a][2]]) #append item to list2 as list (to create row)
            list2[-1].append([list1[i][0]])
            list2[-1].append(list1[i][1])
            print("list1 before elif is: "+str(list1[0]))  #just for testing - everything still ok

        elif list1[i][1][0] in comparsion_list[a][1] and comparsion_list[a][2] == list2[-1][0]:
            print("list1 after elif is: "+str(list1[0])) #just for testing - not ok!
            list2[-1][2].extend(list1[i][1])

但输出如下:

list1 before elif is: ['item1', ['item2'], '0', '0']
list1 after elif is: ['item1', ['item2'], '0', '0']
list1 after elif is: ['item1', ['item2', 'item4'], '0', '0']
list1 after elif is: ['item1', ['item2', 'item4', 'item5', 'item4.5 aaa'], '0', '0']

如您所见,list1 已更改,但我并没有在代码中全部更改!甚至没有引用,因为它始终是深拷贝,并且 deep_of_list1 保持不变。我认为预期的输出应该是这样的:

list1 before elif is: ['item1', ['item2'], '0', '0']
list1 after elif is: ['item1', ['item2'], '0', '0']
list1 after elif is: ['item1', ['item2'], '0', '0']
list1 after elif is: ['item1', ['item2'], '0', '0']

但是当我删除这一行时(来自 elif 语句):

    list2[-1][2].extend(list1[i][1])

那么输出就ok了(第二个)。 这怎么可能?我错过了什么?

【问题讨论】:

  • 请创建一个minimal reproducible example 并从不需要的代码中删除您的问题。
  • 你的代码 sn-p 太乱了,我放弃了去理解它——真的是线路噪音。像deep_of_list1 = deepcopy(list1); list1 = deepcopy(deep_of_list1) 这样的东西真的没有帮助。
  • 为什么你认为你的深拷贝应该保护你免受这种伤害?您显然正在对可变对象进行各种附加等,然后对这些对象进行变异...
  • 好吧,我的问题是,如果没有对其进行更改(没有附加/扩展,它不是参考),那么 list1 怎么可能发生变化,并且当我删除最后一行时,我追加到list2,就ok了……
  • @AndreasBekkelund “这不是参考”是什么意思?您清楚地将.appendlist1 中的项目的引用变为list2,然后您将list2 中的项目变异,这可能是list1 中的相同项目...您不断重复“没有参考”,但是这显然是错误的,所以要么你误解了 deepcopy 的作用,要么存在一些误解

标签: python python-3.x list deep-copy


【解决方案1】:

您将对list1 元素的引用附加到list2。其中一个元素是可变的 (list) 本身。如果您通过显式复制列表来强制创建新的内存对象,您的问题就解决了。

list2[-1].append(list1[i][1].copy()) #in the 'if' part of the code.

除此之外,我可能会重构整个怪物..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多