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!

Article discusses impossibility of tuple comprehension in Python due to syntax ambiguity. Alternatives like using tuple() with generator expressions are suggested for creating tuples efficiently.(159 characters)

The article explains modules and packages in Python, their differences, and usage. Modules are single files, while packages are directories with an __init__.py file, organizing related modules hierarchically.

Article discusses docstrings in Python, their usage, and benefits. Main issue: importance of docstrings for code documentation and accessibility.

Article discusses lambda functions, their differences from regular functions, and their utility in programming scenarios. Not all languages support them.

Article discusses break, continue, and pass in Python, explaining their roles in controlling loop execution and program flow.

The article discusses the 'pass' statement in Python, a null operation used as a placeholder in code structures like functions and classes, allowing for future implementation without syntax errors.

Article discusses passing functions as arguments in Python, highlighting benefits like modularity and use cases such as sorting and decorators.

Article discusses / and // operators in Python: / for true division, // for floor division. Main issue is understanding their differences and use cases.Character count: 158


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

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!
