1.严格来说 append 是追加,把一个东西整体添加在列表后.
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
x.append(['Thursday', 'Sunday'])
print(x)
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', ['Thursday', 'Sunday']]
2.而 extend 是扩展,把一个东西里的所有元素添加在列表后。
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
x.extend(['Thursday', 'Sunday'])
print(x)
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday', 'Sunday']
3.insert指定位置插入元素
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
x.insert(2, 'Sunday')
print(x)
# ['Monday', 'Tuesday', 'Sunday', 'Wednesday', 'Thursday', 'Friday']
4.remove指定删除元素
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
x.remove('Monday')
print(x)
# ['Tuesday', 'Wednesday', 'Thursday', 'Friday']
5.pop弹出元素
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
y = x.pop()
print(y)
# Friday
print(x)
# x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday']
y = x.pop(0)
print(y)
# Monday
y = x.pop(-2)
print(y)
# Wednesday
6.del删除位置元素
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
del x[0]
print(x)
# ['Tuesday', 'Wednesday', 'Thursday', 'Friday']
7.list.count(obj) 统计某个元素在列表中出现的次数 ,obj -- 列表中统计的对象。
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(x.count('Monday'))
# 1
8.reverse将列表倒序
x = [123, 456, 789]
x.reverse()
print(x)
# [789, 456, 123]
以上就是我自己总结的对列表的操作,欢迎补充和指出不足!!!