a[:]=b 和 a=b[:] 有什么区别?

13
a=[1,2,3]
b=[4,5,6]
c=[]
d=[]

这两个语句有什么区别?

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

但两者得到的结果相同。

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

从功能上讲,它们是否有任何区别?

4个回答

34

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

2
非常雄辩地陈述了。(我给你点赞) - mgilson
在功能上有什么区别吗? - Vivek S
@InternalServerError -- 这要看情况。你是否还有其他关于 c 的引用?这些引用也会受到更改的影响。考虑使用:c=[]; f=c; c[:]=a -- 现在,由于 fc 是同一个列表,所以 f 也将具有与 a 相同的元素。 - mgilson
如果变量 a 包含可变对象,则对该可变对象的更改将同时出现在变量 c 中,参见上述示例,返回 @InternalServerError。 - Ashwini Chaudhary
1
@GrijeshChauhan 我正在用两本书学习核心Python编程(第2版)学习Python,但大多数知识都是从SO上的众多优秀人士那里学到的。 - Ashwini Chaudhary
显示剩余3条评论

5

就像Ashwini说的那样。 :) 我稍微补充一下:

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

Ashwini的答案准确描述了发生了什么,以下是两种方法之间差异的几个示例:

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

2

这两者没有太大区别。c[:]=a 直接在原有的列表上进行更新。而 d=b[:] 创建并返回一个新的列表,这个新的列表是 b 的副本(忽略第四行创建的旧列表)。在大多数应用中,除非您有其他对数组的引用,否则您不太可能看到差异。当然,使用 c[:]=... 版本时,您必须已经有一个名为 c 的列表。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接