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 add List2 in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to add List2 in Python". In daily operation, I believe many people have doubts about how to add List2 in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to add List2 in Python". Next, please follow the editor to study!

List has more support for these operations than tuple.

The previous academic committee mentioned that tuple (tuple) is a series of welded carriages, and list supports element editing, which is obviously much more flexible.

Let's take a look at the delete operation first. List in python supports the operation of deleting elements in 3.

Suppose we define a list object, list_obj, then we can do any of the following to delete the element.

Del list_obj [subscript] list_obj.remove (an element value) list_obj.pop (subscript) # returns the element value

OK, let's look at the complete code below:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:36 on 2021-10-31 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Leixue Committee # @ XueWeiTag: CodingDemo# @ File: list_demo4.py# @ Project: hello# Delete the list element mylist = [1,0,2,4, "Ray Learning Committee"] print ("mylist:", mylist) del mylist [0] print ("after remove first element, mylist:" Mylist) mylist.remove ("Lei Xue Commission") print ("after remove first element, mylist:", mylist) removed_value = mylist.pop (1) # removed and returned element value print ("after remove first element, mylist:", mylist) print ("removed value:", removed_value)

The effect is as follows:

It is particularly important to note that deleting elements cannot exceed the subscript range of list, otherwise an error will be reported!

In addition to deleting, how does list add / expand elements?

Not to mention the modification, list also supports location search elements, let's take a look at it first.

List_obj.index (a value of an element) # locates to the first matching subscript through an element value, starting at position 0.

Suppose list_obj = [3,2,1], then what is list_obj.index (2)?

The answer is 1.

Okay, let's move on to inserting new elements.

List in # python supports the following two ways to append the element list_obj.insert (specified subscript, element) list_obj.append (element) # trailing element

What about appending more than one at a time, or directly extending a list to an existing list?

We found the extend function, and we can directly extend the list_obj using list_obj.extend (supplementary list). The effect is to append the elements of the supplementary list to the end in turn.

With so much to do, let's just copy and run the following code:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:36 on 2021-10-31 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Leixue Committee # @ XueWeiTag: CodingDemo# @ File: list_demo5.py# @ Project: other functions in the list mylist = [6,6,6] print ("mylist:", mylist) mylist.append ("Ray Commission") print ("mylist:" Mylist) print ("how many 6s in the list? : ", mylist.count (6)) print (" the location subscript of the first 6? " : ", mylist.index (6) mylist.insert (2, 1024) print (" the first 1024 position subscript? " : ", mylist.index (1024)) last = mylist.pop () # deletion operation mentioned earlier, one of these examples. Print ("the last element is:", last) print ("mylist:", mylist) # directly append the new list mylist.extend (mylist) # equivalent to mylist = mylist * 2print ("mylist:", mylist) mylist.extend (['continuous learning', 'continuous development']) print ("mylist:", mylist)

This is the effect of the code running:

Very simple, let's move on to the sorting of list elements

Sorting of list

All the above are editing operations, and list can also arrange the data, that is, in a certain logical order.

List provides a sort function and a reverse function.

To put it simply, the reverse function is equivalent to turning the whole train car around directly. That is, list: [1Magol 2jue 3] after being processed by the reverse function, it becomes [3Jing 2jue 1].

Sort is more flexible, the default installation element face value (such as numbers, numeric strings), but also supports passing in a lambda function to specify sorting logic.

By default, the above function sorts an array of numbers by number face value.

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:36 on 2021-10-31 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Commission # @ XueWeiTag: CodingDemo# @ File: list_demo6.py# @ Project: other functions in the list mylist = [2,3,1] # mylist = ["2", "3", "1"] mylist.sort () print ("mylist:" Mylist) mylist.extend (['continuous learning', 'continuous development']) print ("mylist:", mylist) mylist.sort (key=lambda e: len (str (e)), reverse=True) print ("sorted mylist:", mylist) mylist.reverse () print ("reversed mylist:", mylist)

The effect is as follows, readers can carefully see if it is as mentioned by the academic Committee.

Special note: the above sample code also shows that if the elements in a list are not of the same type (all numbers, strings or all types), the developer must implement a lambda function to sort the sort function as a reference.

At this point, the study of "how to add List2 in Python" 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

Development

Wechat

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

12
Report