Introduction to common functions of python lists
Introduction to common functions of python lists: 1. append, [names.append('e')]; 2. count statistics, [names.count('a')]; 3. extend; 4 , index gets the subscript value; 5. insert inserts, etc.
Related learning recommendations: python tutorial
1. Definition list
names = [] #定义空列表 names = ['a','b','c'] #定义非空列表
2. Access the elements in the list
>>> names = ['a','b','c','d'] >>> names[0] #访问第一个元素 'a' >>> names[1] #访问第二个元素 'b' >>> names[-1] #访问倒数第一个元素 'd' >>> names[-2] #访问倒数第二个元素 'c'
3. Modify
>>> names = ['a','b','c','d'] >>> names[1] = '1' #对第二个元素进行修改 >>> names ['a', '1', 'c', 'd']
4. Slicing
>>> li = ['a','b','c','d'] >>> li[1:3] #取第二个到第四个之间的元素,包括第二个,不包括第四个 ['b', 'c'] >>> li[1:-1] #取第二个到最后一个之间的元素,包括第二个,不包括最后一个 ['b', 'c'] >>> li[0:3] #取第一个到第四个之间的元素,包括第一个,不包括第四个 ['a', 'b', 'c'] >>> li[:3] #取第一个到第四个之间的元素,同上 ['a', 'b', 'c'] >>> li[3:] #取第四个元素 ['d'] >>> li[-1:] #取最后一个元素 ['d'] >>> li[0::2] #从第一个元素开始,跳着一个取元素 ['a', 'c'] >>> li[::2] #同上 ['a', 'c']
Summary of slicing:
①The sequence is always sliced from left to right, not from right to left
② When slicing the list, the elements in the start position are included, and the elements in the end position are not included (also called the head and not the end), and the last position represents the step size ([start bit:end bit:step])
③If the value is taken from position 0, 0 can be omitted
④When you want to take the last value, the end bit cannot be -1, because the element of the end bit is not included, so it can only be left blank
5.Append
>>> names = ['a','b','c','d'] >>> names.append('e') >>> names ['a', 'b', 'c', 'd', 'e']
6.Statistics(count)
>>> names = ['a','b','c','d','a'] >>> names.count('a') #统计'a'元素的个数 2
7. Extend
>>> names1 = ['a','b','c','d'] >>> names2 = [1,2,3,4] >>> names1.extend(names2) >>> names1 ['a', 'b', 'c', 'd', 1, 2, 3, 4]
Note: The list of names2 is merged into names1, but the list of names2 still exists
8. Get the subscript value (index)
>>> names = ['a','b','c','d'] >>> names.index('a') 0
Note: If there are multiple identical elements in a list, when using index to get the subscript, Only the subscript of the first element will be obtained
9. Insert
>>> names = ['a','b','c','d'] >>> names.insert(0,'1') #0表示需要插入的下标值,'1'表示插入的内容 >>> names ['1', 'a', 'b', 'c', 'd'] #在下标值为0的地方插入'1'
10. Delete (del, pop, remove)
#根据下标值删除元素 >>> names = ['a','b','c','d'] >>> del names[0] >>> names ['b', 'c', 'd'] #根据元素删除 >>> names = ['a','b','c','d'] >>> names.remove('a') >>> names ['b', 'c', 'd'] #删除最后一个 >>> names = ['a','b','c','d'] >>> names.pop() 'd' >>> names ['a', 'b', 'c']
Note: If there is a subscript value in pop(), a specific element is deleted, and its effect is the same as that of del.
11. Reverse
>>> names = ['a','b','c','d'] >>> names.reverse() >>> names ['d', 'c', 'b', 'a'] #将整个列表翻转过来
12. Sort
>>> names = [4,2,3,1] >>> names.sort() >>> names [1, 2, 3, 4]
ps: If you want to view the methods used in the list, you can use dir() to view
Think To learn more about programming learning, please pay attention to the php training column!
The above is the detailed content of Introduction to common functions of python lists. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Using python in Linux terminal...
