【问题标题】:What is the difference between a[:]=b and a=b[:]a[:]=b 和 a=b[:] 有什么区别
【发布时间】:2012-07-03 02:37:35
【问题描述】:
a=[1,2,3]
b=[4,5,6]
c=[]
d=[]

这两种说法有什么区别?

c[:]=a
d=b[:]

但两者都给出相同的结果。

c 是 [1,2,3],d 是 [4,5,6]

在功能方面有什么不同吗?

【问题讨论】:

    标签: python list slice


    【解决方案1】:

    c[:] = a表示将c的所有元素替换为a的元素

    >>> l = [1,2,3,4,5]
    >>> l[::2] = [0, 0, 0] #you can also replace only particular elements using this 
    >>> l
    [0, 2, 0, 4, 0]
    
    >>> k = [1,2,3,4,5]
    >>> g = ['a','b','c','d']
    >>> g[:2] = k[:2] # only replace first 2 elements
    >>> g
    [1, 2, 'c', 'd']
    
    >>> a = [[1,2,3],[4,5,6],[7,8,9]]
    >>> c[:] = a      #creates a shallow copy
    >>> a[0].append('foo') #changing a mutable object inside a changes it in c too
    >>> a
    [[1, 2, 3, 'foo'], [4, 5, 6], [7, 8, 9]]
    >>> c
    [[1, 2, 3, 'foo'], [4, 5, 6], [7, 8, 9]]
    

    d = b[:] 表示创建 b 的浅拷贝并将其分配给 d ,类似于d = list(b)

    >>> l = [1,2,3,4,5]
    >>> m = [1,2,3]
    >>> l = m[::-1] 
    >>> l
    [3,2,1]
    
    >>> l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    >>> m = l[:] #creates a shallow copy 
    >>> l[0].pop(1) # a mutable object inside l is changed, it affects both l and m
    2
    >>> l
    [[1, 3], [4, 5, 6], [7, 8, 9]]
    >>> m
    [[1, 3], [4, 5, 6], [7, 8, 9]]
    

    【讨论】:

    • 说得很有说服力。 (+1 来自我)
    • 在功能方面有什么不同吗?
    • @InternalServerError -- 这取决于。你还有其他对c 的引用吗?这些参考资料也会看到这些变化。考虑:c=[] ; f=c; c[:]=a -- 现在f 也将具有与a 相同的元素,因为fc 是同一个列表。
    • 你的意思是改变a会影响c和f吗?
    • @GrijeshChauhan 我正在学习两本书 Core Python Programming (2nd Edition)Learning Python,但是从 SO 本身的伟人那里学到了很多东西。
    【解决方案2】:

    阿什维尼说的话。 :) 我会详细说明一下:

    In [1]: a=[1,2,3]
    
    In [2]: b = a
    
    In [3]: c = a[:]
    
    In [4]: b, c
    Out[4]: ([1, 2, 3], [1, 2, 3])
    
    In [5]: a is b, a is c
    Out[5]: (True, False)
    

    反之:

    In [1]: a = [1,2,3]
    
    In [2]: aold = a
    
    In [3]: a[:] = [4,5,6]
    
    In [4]: a, aold
    Out[4]: ([4, 5, 6], [4, 5, 6])
    
    In [5]: a = [7,8,9]
    
    In [6]: a, aold
    Out[6]: ([7, 8, 9], [4, 5, 6])
    

    看看会发生什么?

    【讨论】:

      【解决方案3】:

      没有太大区别。 c[:]=a 更新了 c 引用的列表。 d=b[:] 创建一个新列表,它是 b 的副本(忘记您在第 4 行创建的旧列表)。在大多数应用程序中,除非您有其他对数组的引用,否则您不太可能看到差异。当然,对于c[:]=... 版本,您必须已经有一个列表c

      【讨论】:

        【解决方案4】:

        Ashwini's answer 准确地描述了正在发生的事情,以下是两种方法之间差异的几个示例:

        a=[1,2,3]
        b=[4,5,6]
        c=[]
        c2=c
        d=[]
        d2=d
        
        c[:]=a                            # replace all the elements of c by elements of a
        assert c2 is c                    # c and c2 should still be the same list
        c2.append(4)                      # modifying c2 will also modify c
        assert c == c2 == [1,2,3,4]
        assert c is not a                 # c and a are not the same list
        
        d=b[:]                            # create a copy of b and assign it to d
        assert d2 is not d                # d and d2 are no longer the same list
        assert d == [4,5,6] and d2 == []  # d2 is still an empty list
        assert d is not b                 # d and b are not the same list
        

        【讨论】:

          猜你喜欢
          • 2018-06-27
          • 1970-01-01
          • 1970-01-01
          • 2013-06-25
          • 1970-01-01
          • 1970-01-01
          • 2020-10-09
          • 2017-04-06
          • 2010-11-27
          相关资源
          最近更新 更多