python列表的增删改查

my_list=[value,value,....]

my_list.append(value) 追加

my_list.insert(index,value) 中间插入

my_list=['a','b','c','d']
my_list.append('e')
my_list
['a', 'b', 'c', 'd', 'e']
my_list.insert(1,'f')
my_list
['a', 'f', 'b', 'c', 'd', 'e']

my_list 查看全部

my_list[index] 切片

my_list[index1:index2:步长] 切片

my_list.index(value) 查看索引

my_list.count(value) 计算值出现的次数

my_list[::-1] 取反查看

my_list
['a', 'f', 'b', 'c', 'd', 'e']
my_list[2]
'b'
my_list[1:4:1]
['f', 'b', 'c']
my_list.index('b')
2
my_list.count('f')
1
my_list[::-1]
['e', 'd', 'c', 'b', 'f', 'a']
my_list
['a', 'f', 'b', 'c', 'd', 'e']

my_list[index]=value 修改列表的值

my_list.reverse() 取反修改

my_list.pop(index) 删除某个值

my_list.pop(2)
'b'
my_list
['a', 'f', 'c', 'd', 'e']

猜你喜欢

转载自blog.51cto.com/13587169/2122228