Python tutorial: 12 common operation methods for lists

It's all basic knowledge, you will forget it if you don't use it for a long time, learn the new by reviewing the past, and come to learn again. I believe that many people will use it in programming or thinking about some program processing, such as interviews (please correct me if there is something wrong~

1. List definition

A list is an ordered and mutable collection. In Python, lists are denoted by square brackets. The content in the list can be of any type and can be repeated.

li = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 1, 1, 2, 3]
print(li)
['A', 'B', 'C', 'D', 'E', 'F', 'G', 1, 1, 2, 3]

2. Add data to the list

1, add append method

li = ['A', 'B', 'C']
li.append('E')  # 一次只能添加一个元素,并且只能在列表最后
print(li)
['A', 'B', 'C', 'E']

2. Insert the insert method, which is more flexible than append, specify the index bit, and note that the index starts from 0

li = ['A', 'B', 'C']
li.insert(0, 'M')  # 在首位插入M,两个参数
print(li)

['M', 'A', 'B', 'C']

3. Delete the data in the list

1. pop-up method

li = ['A', 'B', 'C']
li.pop()  # 根据索引进行删除,默认删除最后一个元素,一次只能删除一个
print(li)
['A', 'B']

 Delete the data at the specified location

li = ['A', 'B', 'C']
li.pop(0)  # 删除第一位
print(li)
['B', 'C']

2. Remove the remove method

li = ['A', 'B', 'C']
li.remove('B')  # 删除列表中的值,一次只能删除一个
print(li)
['A', 'C']

3. Operation statement del

li = ['A', 'B', 'C']
del li[0] #删指定索引的元素,需要注意是del是一种操作语句
print(li)
['B', 'C']

4. Index (ie subscript)

li = ['A', 'B', 'C']
print(li[0])  # 获取首位
print(li[-1])  # 获取末尾
A
C

5. Slicing, regardless of the head and the tail

li = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 1, 1, 2, 3]
print(li[0:2])  # 获取前两位
['A', 'B']

print(li[:])  # 获取全部
['A', 'B', 'C', 'D', 'E', 'F', 'G', 1, 1, 2, 3]


print(li[4:])  # 获取第5位到尾部
['E', 'F', 'G', 1, 1, 2, 3]

print(li[-3:-1])  # 获取倒数第3到倒数第2
[1, 2]

Six, jumping, involving step size

The main point is to find the index bit, the step size is positive, and the step size is negative, and the step size is reversed

li = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 1, 1, 2, 3]
print(li[1:5:3])  # 从第2位到第6位,隔2个获取
['B', 'E']

print(li[0:5:2])  # 从开头到第6位,隔1个获取
['A', 'C', 'E']

print(li[5:0:-2])  # 从第6位开始,隔1个倒着获取
['F', 'D', 'B']

Seven, commonly used list operators

1. copy *

li = ['A', 'B']
print(li * 2)  # 复制列表中的值追加到列表
['A', 'B', 'A', 'B']

2. Stitching +

li = ['A', 'B']
li2 = ['C', 'D']
print(li + li2)  # 两个列表拼接
['A', 'B', 'C', 'D']

8. Count the number of occurrences of an element in the list

li = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 1, 1, 2, 3]
print(li.count('A'))  # 统计元素A出现的次数
print(li.count(1))  # 统计元素1出现的次数
1
2

Nine, find elements

If there are duplicate elements in the list, the index method only finds the first occurrence of the index bit

li = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 1, 1, 2, 3]
print(li.index('A'))  # 查找元素A首次出现的索引位置
print(li.index(1))  # 查找元素1首次出现的索引位置
0
7

10. Reverse the list

li = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 1, 1, 2, 3]
li.reverse()  # 反转列表元素
print(li)
[3, 2, 1, 1, 'G', 'F', 'E', 'D', 'C', 'B', 'A']

11. List sorting

Only valid for full values, otherwise an error will be reported

li = [1, 5, 2, 99, 6, 44, 66, 3, 34]
li.sort()  # 默认对列表数据进行从小到大排序
print(li)
[1, 2, 3, 5, 6, 34, 44, 66, 99]

li.sort(reverse=True)  # 对列表数据进行从大到小排序
[99, 66, 44, 34, 6, 5, 3, 2, 1]

#根据对列表元素反转的规律,从大到小排序也可以先sort后再reverse
#li.sort()
#li.reverse()

12. Copy list

1. Shallow copy, changing one will affect the other

#浅拷贝
l = ['A', 'B']
k = l
print(k)
l.append('C')
print(l)
print(k)  # 对l列表进行操作会同步影响k列表值

['A', 'B']
['A', 'B', 'C']
['A', 'B', 'C']

2. Deep copy, changing one will not affect the other

# 深拷贝
m = ['A', 'B']
n = m[:]
print(n)
m.append('C')
print(m)
print(n)  # 对m列表进行操作不影响n列表值

['A', 'B']
['A', 'B', 'C']
['A', 'B']

3. Copy function, changing one will not affect the other

m = ['A', 'B']
n = m.copy()
print(n)
m.append('C')
print(n)
['A', 'B']
['A', 'B']

4. The built-in method list, the essence can be understood as converting to a list, changing one will not affect the other

m = ['A', 'B']
n = list(m)
print(n)
m.append('C')
print(n)
['A', 'B']
['A', 'B']

おすすめ

転載: blog.csdn.net/htsssss/article/details/128228811