python copy list

source

references and values

In the Python language, the value stored by a variable is a reference except for the value stored in the basic type (reference refers to the address of the object stored as the value), so you need to be careful about their use, especially copy list and dict. Here is an example:

Problem description: Given a list, seek to generate a new list whose elements are copies of the original list

a=[1,2]
b=a

This approach does not actually generate a new list, and b still points to the object pointed to by a. In this way, if the elements of a or b are modified, the values ​​of a and b will change at the same time.
The solution is:

a=[1,2]
b=a[:]

Modifying a in this way has no effect on b. Modifying b has no effect on a.
But this method is only suitable for simple lists , that is, the elements in the list are all basic types. If there are still lists ( dictionaries ) in the list elements, this method is not applicable. The reason is that a[:]this kind of processing just generates a new list from the value of the list element. If the list element is also a list, such as: a=[1,[2]], then this copying process for element [2] is just copying the reference of [2] , while A new list copy of [2] was not generated. To demonstrate this, the test steps are as follows:

>>> a=[1,[2]]
>>> b=a[:]
>>> b
[1, [2]]
>>> a[1].append(3)
>>> a
[1, [2, 3]]
>>> b
[1, [2, 3]]  # b的值也被修改了

It can be seen that the modification of a affects b. To solve this problem, you can use the deepcopy function in the copy module. Modify the test as follows:
Copy the code The code is as follows:

>>> import copy
>>> a=[1,[2]]
>>> b=copy.deepcopy(a)
>>> b
[1, [2]]
>>> a[1].append(3)
>>> a
[1, [2, 3]]
>>> b
[1, [2]]

Sometimes it's important to know this, because maybe you do need a new list, and operate on that new list without affecting the original list.

deep copy and shallow copy

In fact, the copy list has the following methods, but only deepcopy is a full copy, and the others are shallow copies .

lista = [2,[4,5]]

## 5种拷贝方式:
listb = lista[:]
listb = list(lista)
listb = [i for i in lista]
import copy; listb = copy.copy(lista)
import copy; listb = copy.deepcopy(lista)

# 拷贝后续操作:
listb[1].append(9)
print lista, listb

# 五种拷贝方式后续操作的结果(依次按顺序):
[2, [4, 5, 9]] [2, [4, 5, 9]]
[2, [4, 5, 9]] [2, [4, 5, 9]]
[2, [4, 5, 9]] [2, [4, 5, 9]]
[2, [4, 5, 9]] [2, [4, 5, 9]]
[2, [4, 5]] [2, [4, 5, 9]]  # 只有deepcopy才是深拷贝

In terms of performance (using ipython's %timeit method), the performance of the above methods decreases sequentially:

In [22]: %timeit b=a[:]
10000000 loops, best of 3: 94.5 ns per loop

In [23]: %timeit b=list(a)
10000000 loops, best of 3: 153 ns per loop

In [24]: %timeit b=[i for i in a]
1000000 loops, best of 3: 238 ns per loop

In [27]: %timeit b=copy.copy(a)
The slowest run took 10.53 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 475 ns per loop

In [28]: %timeit b=copy.deepcopy(a)
The slowest run took 5.79 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.36 µs per loop

Summarize

For shallow copy: listb = lista[:], for deep copylistb = copy.deepcopy(lista)

dictionary

The dictionary has its own copy method, but it is also a shallow copy

dicta = {"a": 1}
dictb = dicta.copy()

refer to:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815582&siteId=291194637