码迷,mamicode.com
首页 > 编程语言 > 详细

python列表的增删改查

时间:2018-05-31 00:42:46      阅读:189      评论:0      收藏:0      [点我收藏+]

标签: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列表的增删改查

标签:luchangshan5200

原文地址:http://blog.51cto.com/13587169/2122228

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!