Home>Article>Backend Development> Introduction to common functions of python lists

Introduction to common functions of python lists

coldplay.xixi
coldplay.xixi Original
2020-09-12 11:51:15 8911browse

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.

Introduction to common functions of python lists

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

Introduction to common functions of python lists

Think To learn more about programming learning, please pay attention to thephp trainingcolumn!

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn