Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of referencing, assigning, and copying Python

2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the method of Python citation, assignment and replication". In the daily operation, I believe that many people have doubts about the method of Python citation, assignment and replication. I have consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what is the method of Python citation, assignment and replication?" Next, please follow the editor to study!

Question: in Python, make values= [0minute 1] 2]; values [1] = values, why is the result [0, [...], 2]?

> values = [0,1,2]

> values [1] = values

> values [0, [...], 2]

The expectation should be

[0, [0, 1, 2], 2]

Here's the answer.

Python has no assignment, only references. This is tantamount to creating a structure that references itself, which leads to an infinite loop. In order to understand this problem, there is a basic concept that needs to be clarified.

There are no "variables" in Python, and what we usually call variables are actually just "tags". Execution

Values = [0,1,2]

What Python does is first create a list object [0,1,2] and label it with the name values. If it is followed by execution

Values = [3,4,5]

What Python does is create another list object [3, 4, 5], then tear off the label named values from the previous [0, 1, 2] object and reattach it to [3, 4, 5].

From beginning to end, there is no list object container called values, and Python does not copy the values of any objects into values. The process is shown in the figure

Execution

Values [1] = values

What Python does is point the second element of the list object referenced by the values tag to the list object itself referenced by values. After execution, the values tag still points to the original object, but the structure of that object has changed from the previous list [0,1,2] to [0,1,2], and this? Is a reference to that object itself. As shown in the figure

To achieve the effect you need, that is, to get the object [0, [0,1,2], you can't point values [1] directly to the object referenced by values. Instead, you need to "copy" the object [0,1,2] again to get a new object, and then point values [1] to the copied object. The operation of copying objects in Python varies according to the object type. The operation of copying list values is

Values [:]

So you need to execute

Values [1] = values [:]

What Python does is first dereference to get the object that values points to [0,1,2], then perform the [0,1,2] [:] copy operation to get a new object, the content is also [0,1,2], and then point the second element of the list object pointed to by values to the replicated list object, and finally values points to [0, [0, 1, 2], 2]. The process is shown in the figure:

To go deeper, values [:] copy operations are called "shallow copy", and unexpected errors can occur when list objects are nested, such as

A = [0, [1, 2], 3]

B = a [:]

A [0] = 8

A [1] [1] = 9

Q: what are an and b respectively at this time?

The correct answer is [8, [1, 9], 3] and b is [0, [1, 9], 3]. Did you find out? The second element of b has also been changed. Think about why? If you don't understand, look at the picture below.

The correct way to copy nested elements is to "deep copy" them by

Import copy

A = [0, [1, 2], 3]

B = copy.deepcopy (a)

A [0] = 8

A [1] [1] = 9

At this point, the study of "what is the method of Python citation, assignment and replication" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report