current position:Home>Stroke list in python (bottom)

Stroke list in python (bottom)

2022-01-30 23:35:42 Lei Xuewei

「 This is my participation 11 The fourth of the yuegengwen challenge 2 God , Check out the activity details :2021 One last more challenge

ceremonial Python Column No 27 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !

The first part is the comparison of the Academic Committee tuple The operation of the article is listed one by one list Corresponding operation of list data .

This time we continue to list After reading other operations of .

list Than tuple With more support for these operations

The school committee mentioned earlier tuple( Tuples ) It's a series of welded carriages ,list Support element editing , Obviously much more flexible .

Let's first look at the delete operation ,python Medium list It supports 3 Delete element in .

Suppose we define a list object list_obj, Then we can do any of the following , Delete the element .

del list_obj[ Subscript ]
list_obj.remove( An element value )
list_obj.pop( Subscript ) # Return element value 
 Copy code 

good , Let's look at the complete code below :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : list_demo4.py
# @Project : hello

#  Delete list elements 
mylist = [1, 0, 2, 4, " Lei Xuewei "]
print("mylist:", mylist)
del mylist[0]
print("after remove first element, mylist:", mylist)
mylist.remove(" Lei Xuewei ")
print("after remove first element, mylist:", mylist)
removed_value = mylist.pop(1)  #  Remove and return the element value 
print("after remove first element, mylist:", mylist)
print("removed value:", removed_value)
 Copy code 

The effect is as follows :

 Screenshot  2021-11-02  Afternoon 11.56.09.png

One thing in particular to note : Delete element cannot exceed list Subscript range , Otherwise, the report will be wrong !

In addition to deleting ,list How to add / What about extension elements ?

Not to mention the modified ,list It also supports locating and finding elements , Let's see .

list_obj.index( An element value ) # Locate the first matching subscript by an element value , from 0 The location starts to look for .
 Copy code 

hypothesis list_obj = [3, 2, 1] that list_obj.index(2) What is it ?

The answer is :1.

good , Let's go on to insert new elements .

# python Medium list Support the following two ways to append elements 
list_obj.insert( Specify subscript ,  Elements )
list_obj.append( Elements ) # The last element 
 Copy code 

Then add multiple at one time , Or directly expand a list to an existing list ?

We found it extend function , Use list_obj.extend( Supplementary list ) Can handle list_obj Directly expanded , The effect is to append the elements of the supplementary list to the end in turn .

Say so many operations , Let's copy and run the following code directly to see :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : list_demo5.py
# @Project : hello

#  Other functions of the list 

mylist = [6, 6, 6]
print("mylist:", mylist)
mylist.append(" Lei Xuewei ")
print("mylist:", mylist)
print(" How many lists 6?:", mylist.count(6))
print(" first 6 Location subscript of ?:", mylist.index(6))
mylist.insert(2, 1024)
print(" first 1024 Location subscript of ?:", mylist.index(1024))
last = mylist.pop() # The delete operation has been described earlier , Here are a few examples .
print(" The last element is :",last)
print("mylist:", mylist)


#  Directly append a new list 
mylist.extend(mylist)  #  amount to mylist = mylist * 2
print("mylist:", mylist)
mylist.extend([' Continuous learning ', ' Continuous development '])
print("mylist:", mylist)
 Copy code 

This is the effect of code running :

 Screenshot  2021-11-02  Afternoon 11.57.30.png

It's very simple , Let's continue to look at the sorting of list elements

list Sort

All the above are editing operations ,list You can also arrange data , According to It must be logical Arrange in order .

list Provides a sort Functions and reverse function .

Let's start with the simple ,reverse The function is equivalent to turning the whole train directly . That is to say list:[1,2,3] after reverse After the function , Turned into [3,2,1].

sort More elastic , The default installation element is ( Such as digital , Digital string ), It also supports the introduction of a lambda function , Specifies the sort logic .

By default, the above function will sort an array composed of numbers by the size of their face value

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/31 10:36  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : list_demo6.py
# @Project : hello

#  Other functions of 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)
 Copy code 

The effect is as follows , Readers can take a closer look at whether it is as the school committee said .

 Screenshot  2021-11-02  Afternoon 11.57.30.png

Particular attention : The above example code of the school committee also shows , If the elements in a list are not of the same type ( It's all numbers , Are all strings or some type ), Developers must implement a lambda Function gives sort Function as a reference to sort .

summary

list There are many functions , Operate on elements ( add to / Delete / location ) Waiting is very convenient .

And it can be easily expanded , Sort , Reverse order, etc. , This makes list It is widely used , Every study python Be sure to type more code , Master skillfully .

by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column

Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !

copyright notice
author[Lei Xuewei],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201302335404678.html

Random recommended