简体   繁体   English

Python:列表中相同的项目被视为相同

[英]Python: identical items in list are treated as the same

I have a nested list in python. 我在python中有一个嵌套列表。 Each item the second list is also a nested list. 每个项目的第二个列表也是一个嵌套列表。 My goal is to duplicate one list, insert it right at the same index, and then modify each one. 我的目标是复制一个列表,将其插入相同的索引,然后修改每个列表。 So, example of start condition: 因此,开始条件的示例:

 myList = [[first_list], [[element_1], [element_2, element_3], [duplicate_me]]]

Duplication/insertion at myList[1][2]: 在myList [1] [2]上进行复制/插入:

 myList = [[first_list], [[element_1], [element_2, element_3], [duplicate_me], [duplicate_me]]]

This all works fine. 这一切都很好。 However, when I run the append code: 但是,当我运行附加代码时:

 myList[1][2].append(new_element)

It appends the new element to both duplicates, like this: 它将新元素追加到两个重复项,如下所示:

 myList = [[first_list], [[element_1], [element_2, element_3], [duplicate_me, new_element], [duplicate_me, new_element]]]

Is there something bizarre going on in the way the elements are called or indexed? 元素的调用或索引方式是否有些奇怪? I see a potential workaround (calling the item to be duplicated to a working variable, modifying it there, and then inserting it at the same point), but that seems needlessly complex. 我看到了一种潜在的解决方法(将项目复制到工作变量中,在此处进行修改,然后在同一点插入它),但这似乎不必要地复杂。

Thanks! 谢谢!

myList[1][2] and myList[1][3] don't just have the same values, they are actually the same list. myList [1] [2]和myList [1] [3]不仅具有相同的值,它们实际上是相同的列表。 You are looking at the same area in memory when you read both of them. 读取它们时,您正在看的是内存中的同一区域。 So, when you change one, you are changing the other, because both are actually the exact same thing! 因此,当您更改一个时,您也在更改另一个,因为两者实际上是完全一样的! Instead doing what you do to duplicate the list, you should make a copy of it. 与其重复复制列表,不如做一个副本。

Here's an example of the problem from a python shell: 这是一个来自python shell的问题的示例:

>>> mylist = [1, 2, 3]
>>> newlist = mylist
>>> newlist.append(4)
>>> newlist
[1, 2, 3, 4]
>>> mylist
[1, 2, 3, 4]

A common way to fix this is to use one of these tricks: 解决此问题的常用方法是使用以下技巧之一:

>>> mylist = [1, 2, 3]
>>> newlist = mylist[:]  # OR :
>>> newlist = [x for x in mylist]
>>> newlist.append(4)
>>> newlist
[1, 2, 3, 4]
>>> mylist
[1, 2, 3]

The second and third lines will create a copy of the list. 第二和第三行将创建列表的副本。 I tend to combine the two like this, if I need to copy a 2D list: 如果我需要复制2D列表,则倾向于将两者结合起来:

>>> newlist = [x[:] for x in mylist]

Use one of these to create your duplicate, and all will be well again. 使用其中之一创建您的副本,一切都会好起来的。

You are most likely not duplicating the target list (*[duplicate_me]*), but rather simply appending a duplicated reference to the same list into myList . 您很可能不会复制目标列表(* [duplicate_me] *),而只是将对同一列表的重复引用附加到myList中

You need to copy the list before adding it to myList. 您需要先复制列表,然后再将其添加到myList中。 One simple way is to call the list constructor passing in the original [duplicate_me] 一种简单的方法是调用传入原始[duplicate_me]的列表构造函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM