Talking about the usage of list.copy method in python

 

This article mainly introduces the relevant information about the use of the list.copy method in python. The article also introduces the difference between python list.copy() and copy.deepcopy(). Friends who need it can refer to the following

When we want to copy two identical lists, we may use the list.copy() method, which allows us to copy an identical array. When encountering the following situation, we may encounter some problems

1

2

3

4

5

# _*_coding='utf8'_*_

nameList = [1, 2, 3, 4, 5]

nameList1 = nameList.copy()

nameList[1] = 55

print(nameList, nameList1)

At this time, when nameList and nameList1 are printed out, the elements of the two lists are as follows

[1, 55, 3, 4, 5] [1, 2, 3, 4, 5]

You can see that the second element of the list is different

This is because when the function list.copy() in python is executed, it does not point to the address of nameList in memory, but a new copy. At this time, the addresses of the two lists in memory are different. We can print it out

Use the following code to see that the memory of the two lists is already different

1

2

3

4

nameList = [1, 2, 3, [3, 4, 5], 4, 5]

nameList1 = nameList.copy()

nameList[1] = 55

print(id(nameList), id(nameList1))

2207528867520 2207531826048

At this time, the modified nameList[1] = 55 will not take effect in nameList1

And when there is an array in the nameList, the situation will change again

1

2

3

4

5

# _*_coding='utf8'_*_

nameList = [1, 2, 3, [3, 4, 5], 4, 5]

nameList1 = nameList.copy()

nameList[3][2] = 55

print(nameList, nameList1)

Enter the following content

[1, 2, 3, [3, 4, 55], 4, 5] [1, 2, 3, [3, 4, 55], 4, 5]

It can be seen that the nested arrays in the two lists have changed. This is because the two-dimensional list stored in the list actually stores the address of the two-dimensional list in memory. When one of the lists is modified, the other Naturally, it will also be affected, so let us verify whether it is the stored memory address.

Similarly, we use the id() method to retrieve the memory address

1

2

3

4

5

# _*_coding='utf8'_*_

nameList = [1, 2, 3, [3, 4, 5], 4, 5]

nameList1 = nameList.copy()

nameList[3][2] = 55

print(id(nameList[3]), id(nameList1[3]))

output the following

2879859422336 2879859422336

It can be seen that the two memory addresses are the same

This is the end of the copy method introduction

Supplement: difference between python list.copy() and copy.deepcopy()

for example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import copy

class ListNode:

    def __init__(self, x):

        self.val = x

        self.next = None

    def __repr__(self):

        return str(self.val)

list1 = [ListNode(1), ListNode(2)]

list2 = list1.copy()

list3 = list1[:]

list4 = copy.deepcopy(list1)

print(list1, list2, list3, list4)

# [1, 2] [1, 2] [1, 2] [1, 2]

list1[0].val = 5

print(list1, list2, list3, list4)

# [5, 2] [5, 2] [5, 2] [1, 2]

list1.append(ListNode(10))

print(list1, list2, list3, list4)

# [5, 2, 10] [5, 2] [5, 2] [1, 2]

print(id(list1[0]),id(list2[0]),id(list3[0]),id(list4[0]))

# 1984073709792 1984073709792 1984073709792 1984073707824

explain

  • list1 is the original array
  • Both list2 and list3 are shallow copies of list1. What does shallow copy mean?

python的list里面存的都是引用,如果存的是listnode,实际上存的是listnode的引用,也就是地址,毕竟如果listnode里存了很多关于这个node的信息的话,直接在list里面存这段信息的地址就很方便,等到要用这段信息的时候(比如list1[0].val),只要找到地址(list1[0])再到这段地址取值(.val)就可以了。

浅拷贝的意思是,开辟一段内存,这段内存里复制了原list的地址。

地址还是那个地址,所以可以看出**id(list1[0]),id(list2[0])**是完全一样的。这会带来的问题是,当我们把这段地址里的值.val变化一下(list1[0].val = 5), 浅拷贝的数组里对应元素的值也就跟着变了

深拷贝就厉害了,不光把原数组存的地址拷贝了,就连原数组地址里对应的数据也都进行了复制,所以可以看到**id(list1[0]),id(list4[0])**变得不一样了。

转自:微点阅读   https://www.weidianyuedu.com

Guess you like

Origin blog.csdn.net/weixin_45707610/article/details/131878086