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 are the five list copy methods in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the five list copying methods in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Assignment operation

The easiest thing to think of is that we can use the assignment operation to copy the list directly.

The code is as follows:

Copied_list=original_list

At this point, both original_list and copyed_list will point to the same list object.

Examples are as follows:

Original_list= [1Jing 2jue 3] # Copying list using assignment operationcopied_list=original_listprint (copied_list) # Output: [1je 2je 3] print (original_list) # Output: [1Met 2je 3] # checking the id of both original and copied listprint (id (original_list)) # Output:print (id (copied_list)) # Output:26751688

The above figure is visualized as follows:

At this point, changes made in original_list will be reflected in copyed_list and vice versa.

The sample code is as follows:

# modifying original_listoriginal_list.append (99) # Output: [1,2,3 id 99] print (copied_list) # Output: [1,2,3 Magazine 99] print (id (original_list)) # Output:26751688print (id (copied_list)) # Output:26751688

The visualization results are as follows:

two。 Use copy operation

We can also use the copy () function to copy the python list, where original_list and copyed_list point to different list objects in memory.

The sample code is as follows:

Original_list= [1Met 2 original_list 3] # Copying list using copy functioncopied_list=original_list.copy () print (copied_list) # Output: [1 Mei 2 Mei 3] print (original_list) # Output: [1 Mei 2 Mei 3] # checking the id of both original and copied listprint (id (original_list)) # Output:27800264print (id (copied_list)) # Output:27799880

The visualization is as follows:

Because the two point to memory differently, the changes made in the original list at this time are not reflected in the copied list, and vice versa.

# modifying original_listoriginal_list.append (99) print (original_list) # Output: [1,2,3 copied_list 99] print (copied_list) # Output: [1,2,3] print (id (copied_list)) # Output:27799880print (id (original_list)) # Output:27800264

The visualization is as follows:

3. Use the list () constructor

We can also use the list () constructor to copy the list. Both original_list and copyed_list point to different list objects. The code is as follows:

Copied_list=list (original_list)

The sample code is as follows:

Original_list= [1id 2 checking the id of both original and copied listprint 3] # Copying list using list () constructorcopied_list=list (original_list) print (copied_list) # Output: [1 original_list 2 Mei3] print (original_list) # Output: [1 Mei 2 Magi 3] # checking the id of both original and copied listprint (id (original_list)) # Output:27800264print (id (copied_list)) # Output:27799880

At this point, the elements in the modified original_list are not reflected in the copyed_list, and vice versa.

# modifying original_listoriginal_list.append (99) print (original_list) # Output: [1,2,3 print 99] print (copied_list) # Output: [1,2,3] 4. Use index

Next, we can use the index to copy the list.

S [i:j:k]-slice of s from i to j with step k

At this point, iMagol jjjjjjjjjrek explains as follows:

I → start index, j → end index, k → step

If we use original_list [:], it will slice the original_list from beginning to end and return a copy of the list.

As follows:

Copied_list=original_list [:]

Then let's look at an example:

Original_list= [1Met 2 id 3] # Copying list using INDEXINGcopied_list=original_list [:] print (copied_list) # Output: [1 Mei 2 Mei 3] print (original_list) # Output: [1 Mei 2 Mei 3] # checking the id of both original and copied listprint (id (original_list)) # Output:27800264print (id (copied_list)) # Output:27799880

At this point, the modified orignial_list is not reflected in the copyed_list, and vice versa.

# modifying original_listoriginal_list.append (99) print (original_list) # Output: [1,2,3 print (copied_list) # Output: [1,2,3] print (id (copied_list)) # Output:27799880print (id (original_list)) # Output:278002645. List generation

Finally, we can use list generation to copy the elements in the list

The code is as follows:

Copied_list= [i for i in original_list]

Let's take a chestnut. The code is as follows:

Original_list= [1Met 2 original_list 3] # Copying list using list comprehensioncopied_list= [i for i in original_list] print (copied_list) # Output: [1 Mei 2 Mei3] print (original_list) # Output: [1 Mei 2 Mei 3] # checking the id of both original and copied listprint (id (original_list)) # Output:27800264print (id (copied_list)) # Output:27799880

At this point, the modified original_list is not reflected in the copyed_list, and vice versa.

# modifying original_listoriginal_list.append (99) print (original_list) # Output: [1,2,3 print (copied_list) # Output: [1,2,3] print (id (copied_list)) # Output:27799880print (id (original_list)) # Output:27800264 "what are the five list copy methods in Python?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report