复制列表(或多数内置的可变集合)最简单的方式是使用内置的类型构造方法。如l2 = list( l1 )
对列表和其他可变序列来说,还能使用简洁的l2 = l1[ : ]语句创建副本。
然而,构造方法或[:]做的是浅复制(即复制了最外层容器,副本中的元素是源容器中元素的引用)。如果所有元素都是不可变的,那么这样没有问题,还能节省内存。但是,如果有可变的元素,可能就会导致意想不到的问题。
深复制(即副本不共享内部对象的引用)。copy模块提供的deepcopy和copy函数能为任意对象做深复制和浅复制。
import copy copy.copy() copy.deepcopy()
l1= [3,[66,55,44], (7,8,9)] l2 = list(l1) l3 = l1 print("l2 is l1:", l2 is l1) print('l3 is l1:', l3 is l1) print(f''' l1[0] address: {id(l1[0])}, l2[0] address:{id(l2[0])} l1[1] address: {id(l1[1])}, l2[1] address:{id(l2[1])} l1[2] address: {id(l1[2])}, l2[2] address:{id(l2[2])}''') l1.append(100) l1[1].remove(55) print("\nl1追加元素100对l2没有影响,但移除l1[1]中的元素55对l2有影响,因为它们指向同一个地址:") print(f' l1 content:{l1}\n l2 content:{l2}') print("\nl2[1]添加[33,22]:") l2[1] += [33,22] print(f' l1 content:{l1}\n l2 content:{l2}') #l1和l2,谁对[1]中的列表进行修改,相应的都会在另一方看到对应的修改 print("\nl2[2] += (10,11)修改后:") l2[2] += (10,11) print(f' l1 content:{l1}\n l2 content:{l2}') print("\n元组使用+=会创建一个新元组,可以看到现在l1[2]和l2[2]位置上不是同一个对象:") print(f''' l1[0] address: {id(l1[0])}, l2[0] address:{id(l2[0])} l1[1] address: {id(l1[1])}, l2[1] address:{id(l2[1])} l1[2] address: {id(l1[2])}, l2[2] address:{id(l2[2])}''')
运行结果:
import copy class Bus: def __init__(self, passengers = None): if passengers is None: self.passengers = [] else: self.passengers = list(passengers) def pick(self, name): self.passengers.append(name) def drop(self, name): self.passengers.remove(name) bus1 = Bus(['Alice', 'Bill', 'Claire', 'David']) bus2 = copy.copy(bus1) bus3 = copy.deepcopy(bus1) print(id(bus1), id(bus2), id(bus3)) print(id(bus1.passengers), id(bus2.passengers), id(bus3.passengers)) bus4 = Bus(['Alice', 'Bill', 'Claire', 'David']) print(id(bus4), id(bus4.passengers))
运行结果:
标签:passengers,l1,复制,l2,address,print,id From: https://www.cnblogs.com/bravesunforever/p/17363804.html