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 :
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 :
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 .
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
The sidebar is recommended
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
guess what you like
-
Python notes (22): errors and exceptions
-
Python has been hidden for ten years, and once image recognition is heard all over the world
-
Python notes (21): random number module
-
Python notes (19): anonymous functions
-
Use Python and OpenCV to calculate and draw two-dimensional histogram
-
Python, Hough circle transformation in opencv
-
A library for reading and writing markdown in Python: mdutils
-
Datetime of Python time operation (Part I)
-
The most useful decorator in the python standard library
-
Python iterators and generators
Random recommended
- [Python from introduction to mastery] (V) Python's built-in data types - sequences and strings. They have no girlfriend, not a nanny, and can only be used as dry goods
- Does Python have a, = operator?
- Go through the string common sense in Python
- Fanwai 4 Handling of mouse events and solutions to common problems in Python opencv
- Summary of common functions for processing strings in Python
- When writing Python scripts, be sure to add this
- Python web crawler - Fundamentals (1)
- Pandas handles duplicate values
- Python notes (23): regular module
- Python crawlers are slow? Concurrent programming to understand it
- Parameter passing of Python function
- Stroke tuple in Python
- Talk about ordinary functions and higher-order functions in Python
- [Python data acquisition] page image crawling and saving
- [Python data collection] selenium automated test framework
- Talk about function passing and other supplements in Python
- Python programming simulation poker game
- leetcode 160. Intersection of Two Linked Lists (python)
- Python crawler actual combat, requests module, python to grab the beautiful wallpaper of a station
- Fanwai 5 Detailed description of slider in Python opencv and solutions to common problems
- My friend's stock suffered a terrible loss. When I was angry, I crawled the latest data of securities with Python
- Python interface automation testing framework -- if you want to do well, you must first sharpen its tools
- Python multi thread crawling weather website pictures and saving
- How to convert pandas data to excel file
- Python series tutorials 122
- Python Complete Guide - printing data using pyspark
- Python Complete Guide -- tuple conversion array
- Stroke the list in python (top)
- Analysis of Python requests module
- Comments and variables in Python
- New statement match, the new version of Python is finally going to introduce switch case?
- Fanwai 6 Different operations for image details in Python opencv
- Python crawler native code learning (I)
- Python quantitative data warehouse building series 2: Python operation database
- Python code reading (Part 50): taking elements from list intervals
- Pyechart + pandas made word cloud pictures of 19 report documents
- [Python crawler] multithreaded daemon & join() blocking
- Python crawls cat pictures in batches to realize thousand image imaging
- Van * Python | simple crawling of a planet
- Input and output of Python practice