list和list [:]在python中有什么区别?

时间:2022-06-20 13:25:42

What, if any, is the difference between list and list[:] in python?

什么,如果有的话,是python中列表和列表[:]之间的区别?

6 个解决方案

#1


63  

When reading, list is a reference to the original list, and list[:] shallow-copies the list.

读取时,list是对原始列表的引用,list [:]浅表复制列表。

When assigning, list (re)binds the name and list[:] slice-assigns, replacing what was previously in the list.

分配时,列表(重新)绑定名称并列出[:] slice-assigns,替换先前列表中的内容。

Also, don't use list as a name since it shadows the built-in.

此外,不要使用列表作为名称,因为它会影响内置。

#2


19  

The latter is a reference to a copy of the list and not a reference to the list. So it's very useful.

后者是对列表副本的引用,而不是对列表的引用。所以它非常有用。

>>> li = [1,2,3]
>>> li2 = li
>>> li3 = li[:]
>>> li2[0] = 0
>>> li
[0, 2, 3]
>>> li3
[1, 2, 3]

#3


3  

To apply the first list to a variable will create a reference to the original list. The second list[i] will create a shallow copy.

要将第一个列表应用于变量,将创建对原始列表的引用。第二个列表[i]将创建一个浅表副本。

for example:

例如:

foo = [1,2,3]
bar = foo
foo[0] = 4

bar and foo will now be:

bar和foo现在将是:

[4,2,3]

but:

但:

foo = [1,2,3]
bar = foo[:]
foo[0] = 4

result will be:

结果将是:

bar == [1,2,3]
foo == [4,2,3]

: is to slice.

:是切片。

#4


3  

However, if the list elements are lists themselves, even list1 = list[:] has its problems. Consider:

但是,如果列表元素本身就是列表,那么即使list1 = list [:]也存在问题。考虑:

>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> b = a[:]
>>> b[0].remove(2)
>>> b 
[[1, 3], [4, 5, 6], [7, 8, 9]]
>>> a
[[1, 3], [4, 5, 6], [7, 8, 9]]

This happens because each list element being copied to b is a list itself, and this copying of lists involves the same problem that occurs with the normal list1 = list2. The shortest way out that I've found is to explicitly copy every list element this way:

发生这种情况是因为复制到b的每个列表元素本身就是一个列表,这种列表复制涉及与普通list1 = list2相同的问题。我发现的最短路径是以这种方式显式复制每个列表元素:

>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> b=[[j for j in i] for i in a]   
>>> b
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b[0].remove(2)
>>> b
[[1, 3], [4, 5, 6], [7, 8, 9]]
>>> a
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Of course, for every additional degree of nesting in the nested list, the copying code deepens by an additional inline for loop.

当然,对于嵌套列表中的每个额外嵌套程度,复制代码通过额外的内联循环加深。

#5


1  

li[:] creates a copy of the original list. But it does not refer to the same list object. Hence you don't risk changing the original list by changing the copy created by li[:].

li [:]创建原始列表的副本。但它没有引用相同的列表对象。因此,您无需通过更改li [:]创建的副本来更改原始列表。

for example:

例如:

>>> list1 = [1,2,3]
>>> list2 = list1
>>> list3 = list1[:]
>>> list1[0] = 4
>>> list2
[4, 2, 3]
>>> list3
[1, 2, 3]

Here list2 is changed by changing list1 but list3 doesn't change.

这里通过更改list1来更改list2,但list3不会更改。

#6


0  

The first one references to the original list. The second one points to the copy of the original list.
Check this out!

第一个引用原始列表。第二个指向原始列表的副本。看一下这个!

>>> a = [1, 2, 3]
>>> b = a
>>> c = a[:]
>>> a == b
True
>>> a is b
True
>>> a == c
True
>>> a is c
False
>>> a.__repr__
<method-wrapper '__repr__' of list object at 0x7f87a9ba3688>
>>> a.__repr__()
'[1, 2, 3]'
>>> b.__repr__
<method-wrapper '__repr__' of list object at 0x7f87a9ba3688>
>>> c.__repr__
<method-wrapper '__repr__' of list object at 0x7f87ad352988>

Notice that both a and b point to the address 0x7f87a9ba3688 whereas, c points to 0x7f87ad352988.
The difference is crystal clear.
Both a and b reference to the original list object.
Whereas, c points to the copy (of the original list) and thus, it is in a different location.

请注意,a和b都指向地址0x7f87a9ba3688,而c指向0x7f87ad352988。差异非常明显。 a和b都引用原始列表对象。然而,c指向(原始列表的)副本,因此,它位于不同的位置。

#1


63  

When reading, list is a reference to the original list, and list[:] shallow-copies the list.

读取时,list是对原始列表的引用,list [:]浅表复制列表。

When assigning, list (re)binds the name and list[:] slice-assigns, replacing what was previously in the list.

分配时,列表(重新)绑定名称并列出[:] slice-assigns,替换先前列表中的内容。

Also, don't use list as a name since it shadows the built-in.

此外,不要使用列表作为名称,因为它会影响内置。

#2


19  

The latter is a reference to a copy of the list and not a reference to the list. So it's very useful.

后者是对列表副本的引用,而不是对列表的引用。所以它非常有用。

>>> li = [1,2,3]
>>> li2 = li
>>> li3 = li[:]
>>> li2[0] = 0
>>> li
[0, 2, 3]
>>> li3
[1, 2, 3]

#3


3  

To apply the first list to a variable will create a reference to the original list. The second list[i] will create a shallow copy.

要将第一个列表应用于变量,将创建对原始列表的引用。第二个列表[i]将创建一个浅表副本。

for example:

例如:

foo = [1,2,3]
bar = foo
foo[0] = 4

bar and foo will now be:

bar和foo现在将是:

[4,2,3]

but:

但:

foo = [1,2,3]
bar = foo[:]
foo[0] = 4

result will be:

结果将是:

bar == [1,2,3]
foo == [4,2,3]

: is to slice.

:是切片。

#4


3  

However, if the list elements are lists themselves, even list1 = list[:] has its problems. Consider:

但是,如果列表元素本身就是列表,那么即使list1 = list [:]也存在问题。考虑:

>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> b = a[:]
>>> b[0].remove(2)
>>> b 
[[1, 3], [4, 5, 6], [7, 8, 9]]
>>> a
[[1, 3], [4, 5, 6], [7, 8, 9]]

This happens because each list element being copied to b is a list itself, and this copying of lists involves the same problem that occurs with the normal list1 = list2. The shortest way out that I've found is to explicitly copy every list element this way:

发生这种情况是因为复制到b的每个列表元素本身就是一个列表,这种列表复制涉及与普通list1 = list2相同的问题。我发现的最短路径是以这种方式显式复制每个列表元素:

>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> b=[[j for j in i] for i in a]   
>>> b
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b[0].remove(2)
>>> b
[[1, 3], [4, 5, 6], [7, 8, 9]]
>>> a
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Of course, for every additional degree of nesting in the nested list, the copying code deepens by an additional inline for loop.

当然,对于嵌套列表中的每个额外嵌套程度,复制代码通过额外的内联循环加深。

#5


1  

li[:] creates a copy of the original list. But it does not refer to the same list object. Hence you don't risk changing the original list by changing the copy created by li[:].

li [:]创建原始列表的副本。但它没有引用相同的列表对象。因此,您无需通过更改li [:]创建的副本来更改原始列表。

for example:

例如:

>>> list1 = [1,2,3]
>>> list2 = list1
>>> list3 = list1[:]
>>> list1[0] = 4
>>> list2
[4, 2, 3]
>>> list3
[1, 2, 3]

Here list2 is changed by changing list1 but list3 doesn't change.

这里通过更改list1来更改list2,但list3不会更改。

#6


0  

The first one references to the original list. The second one points to the copy of the original list.
Check this out!

第一个引用原始列表。第二个指向原始列表的副本。看一下这个!

>>> a = [1, 2, 3]
>>> b = a
>>> c = a[:]
>>> a == b
True
>>> a is b
True
>>> a == c
True
>>> a is c
False
>>> a.__repr__
<method-wrapper '__repr__' of list object at 0x7f87a9ba3688>
>>> a.__repr__()
'[1, 2, 3]'
>>> b.__repr__
<method-wrapper '__repr__' of list object at 0x7f87a9ba3688>
>>> c.__repr__
<method-wrapper '__repr__' of list object at 0x7f87ad352988>

Notice that both a and b point to the address 0x7f87a9ba3688 whereas, c points to 0x7f87ad352988.
The difference is crystal clear.
Both a and b reference to the original list object.
Whereas, c points to the copy (of the original list) and thus, it is in a different location.

请注意,a和b都指向地址0x7f87a9ba3688,而c指向0x7f87ad352988。差异非常明显。 a和b都引用原始列表对象。然而,c指向(原始列表的)副本,因此,它位于不同的位置。