1. 程式人生 > >python列表的增刪改查

python列表的增刪改查

luchangshan5200

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‘]

python列表的增刪改查