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

How to implement the add operation in the list by python

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

Share

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

Editor to share with you how python to achieve the list of add operations, I believe that most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

An increase in the list

There are two ways to add to a list, one is to create a new list, and the other is to add a new element to an existing list. Let's start with how to create a new list:

# create an empty list list1 = [] # create a default value of 0 A list of length n list2 = [0] * n # create a list list3 = [i for i in range (10)] # 5room5 2D list list4 = [[i for i in range (5)] for j in range (5)] # generate a list using other data structures, and the list () function receives an iterable object as a parameter tuple1 = (1,2) 3) list5 = list (tuple1) # string is converted into list str1 = "I love code" # each element as an element in the list list6 = list (str1) # is divided by specified characters list7 = str1.split (") # ['Illustrated,' love', 'code']

In the process of programming, sometimes we need to operate on a list, but the list is a variable object, and the operation on the list often changes its original order structure. Therefore, when we do not want to change the order structure of the list, we need to make a copy of the old list and then operate on the new list. There are two types of copies, shallow copy and deep copy. Some people may have questions about these two copies. don't they get the same form of list?

Yes, they do get the same form of list after they are copied. But there will be a big difference when operating, especially if there are mutable objects in your list. We just need to remember that shallow copies only copy immutable objects, while deep copies copy not only immutable objects, but also mutable objects. The following examples are given:

# if the elements in a list are immutable objects, you can directly copy the list with list1 = [1,2,'i'] list2 = list1 [:: 1] # shallow copy list3 = [i for i in list1] # shallow copy import copylist4 = copy.copy (list1) # shallow copy # if a list element contains mutable objects, complete the copy list A deep copy of list5 = ["Will", 1, ["Python", "Java", "C++"]] list6 = copy.deepcopy (list5) is required

Why do you need a deep copy when containing immutable objects?

As we said above, shallow copies only copy immutable objects. When there are mutable objects in the list, we can find that the references to id are the same, that is, they are the same object, so the operation on list5 affects the list6. In deep copies, for mutable objects, a new object is generated in the new list of copies, so changes to list6 do not affect list5.

Id (list5 [2]) # 2195936916360id (list6 [2]) # 2195936916744

Another addition to the list is to add elements to the existing list, which mainly includes the following operations:

List1 = ["I"] # add a new element list1.append ("love") # merge two lists list2 = ["Apple", 3] list1.extend (list2) # ["I", "love", "Apple", 3] list1 = list1 + list2 # ["I", "love", "Apple", 3] # insert the element list1.insert (index=1, "not") # ["I", "not") at the specified location "love", "Apple", 3] these are all the contents of the article "how python implements additions in the list" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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