Python append() 与深拷贝、浅拷贝

在leetcode77中,发现list.append的结果不对。原代码:

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        res = []
        path = []
        def backtracing(n, start_i):
            if len(path) == k:
                res.append(path)
                return
            
            for i in range(start_i, n+1):
                path.append(i)
                backtracing(n, i+1)
                path.pop()
        
        backtracing(n,  1)
        return res

错误是:输出总是[ [], [],[], ... ,[]]。因为path在过程中肯定长度到达过k,否则不会res.append。思考了一下觉得可能是list.append()追加的是引用,所以当最后path变为[]时res随之变化。查了一下资料,发现list.append确实追加的是引用:

>>>alist = []
>>> num = [2]
>>> alist.append( num )
>>> id( num ) == id( alist[0] )
True

如果希望是深拷贝,则需要写成:

list.append( copy.deepcopy( num ) )

leetcode77中,因为对象是一维列表,所以浅拷贝就够了。

另附:直接赋值、浅拷贝、深拷贝:

  • 直接赋值:其实就是对象的引用(别名)。

  • 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象。

  • 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象。

import copy
a = [1, 2, 3, 4, ['a', 'b']] #原始对象
 
b = a                       #赋值,传对象的引用
c = copy.copy(a)            #对象拷贝,浅拷贝
d = copy.deepcopy(a)        #对象拷贝,深拷贝
 
a.append(5)                 #修改对象a
a[4].append('c')            #修改对象a中的['a', 'b']数组对象
 
print( 'a = ', a )
print( 'b = ', b )
print( 'c = ', c )
print( 'd = ', d )

执行结果为:

('a = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5])
('b = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5])
('c = ', [1, 2, 3, 4, ['a', 'b', 'c']])
('d = ', [1, 2, 3, 4, ['a', 'b']])

 

参考:https://www.runoob.com/w3cnote/python-append-deepcopy.html

https://www.runoob.com/w3cnote/python-understanding-dict-copy-shallow-or-deep.html

posted @ 2021-02-09 16:03  bokeyuan6  阅读(639)  评论(0编辑  收藏  举报