简体   繁体   中英

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. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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