简体   繁体   English

Python:为什么我的原始列表在更新复制列表后会受到影响

[英]Python: why my original list is affected after updating copied list

twoSeq=[['28.406925,77.285590', '28.409969,77.292279'],
    ['28.406925,77.285590', '28.402476,77.292956'],
    ['28.409969,77.292279', '28.403020,77.298851'],
    ['28.403020,77.298851', '28.392363,77.306091'],
    ['28.392363,77.306091', '28.378515,77.313990'],
    ['28.378515,77.313990', '28.367469,77.315308'],
    ['28.402476,77.292956', '28.399600,77.297313'],
    ['28.402476,77.292956', '28.397301,77.294096'],
    ['28.399600,77.297313', '28.392247,77.301909'],
    ['28.392247,77.301909', '28.392363,77.306091'],
    ['28.397301,77.294096', '28.399600,77.297313']]

def N_Seq(twoSeq):
    first=twoSeq.copy()
    last=twoSeq.copy()

    for i in range(len(first)):
        first[i].pop(0)
    print(first,"--------")
    for j in range(len(last)): 
        last[j].pop() 
    print(first)
N_Seq(twoSeq)

Output:输出:

[['28.409969,77.292279'], ['28.402476,77.292956'], ['28.403020,77.298851'], ['28.392363,77.306091'], ['28.378515,77.313990'], ['28.367469,77.315308'], ['28.399600,77.297313'], ['28.397301,77.294096'], ['28.392247,77.301909'], ['28.392363,77.306091'], ['28.399600,77.297313']] --------
[[], [], [], [], [], [], [], [], [], [], []]

list.copy creates a shallow copy of the list. list.copy创建列表的浅拷贝。 This means that it creates a new list and inserts references to items into it.这意味着它会创建一个新列表并将对项目的引用插入其中。 In this case you should usecopy.deepcopy , which returns a deep copy.在这种情况下,您应该使用copy.deepcopy ,它返回一个深层副本。

From the documentation:从文档:

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):浅拷贝和深拷贝的区别仅与复合对象(包含其他对象的对象,如列表或类实例)有关:

  • A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.浅拷贝构造一个新的复合对象,然后(在可能的范围内)向其中插入原始对象中的对象的引用

  • A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.深拷贝构造一个新的复合对象,然后递归地将原始对象中的对象的副本插入其中。

Shallow list copy浅表副本

A shallow copy only copies the list itself, which is a container of references to the objects in the list.浅拷贝仅复制列表本身,它是对列表中对象的引用的容器。 If the objects contained themselves are mutable and one is changed, the change will be reflected in both lists.如果包含自己的对象是可变的,并且其中一个被更改,则更改将反映在两个列表中。

Deep copy深拷贝

The deep copied list is an entirely different list from the original.深复制列表与原始列表完全不同。

import copy
new_list = copy.deepcopy(old_list)

For reference .参考

import copy
twoSeq=[['28.406925,77.285590', '28.409969,77.292279'],
['28.406925,77.285590', '28.402476,77.292956'],
['28.409969,77.292279', '28.403020,77.298851'],
['28.403020,77.298851', '28.392363,77.306091'],
['28.392363,77.306091', '28.378515,77.313990'],
['28.378515,77.313990', '28.367469,77.315308'],
['28.402476,77.292956', '28.399600,77.297313'],
['28.402476,77.292956', '28.397301,77.294096'],
['28.399600,77.297313', '28.392247,77.301909'],
['28.392247,77.301909', '28.392363,77.306091'],
['28.397301,77.294096', '28.399600,77.297313']]

def N_Seq(twoSeq):
    first=copy.deepcopy(twoSeq)
    last=copy.deepcopy(twoSeq)

    for i in range(len(first)):
        first[i].pop(0)
    print(first,"--------")
    for j in range(len(last)): 
        last[j].pop() 
    print(first)
N_Seq(twoSeq)

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

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