2019-04-14
阅读量:
674
python列表insert方法解析
python中向列表对象插入数据可以使用insert方法,其语法如下:
insert(index,object)
参数说明:
index:索引,用来指示插入对象的位置,将数据插入到指定索引的元素前
object:被插入的数据对象
当index为正数的时候,向索引位置元素前添加object
ls = [_ for _ in range(10)]
ls
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ls.insert(1,"a")
ls
[0, 'a', 1, 2, 3, 4, 5, 6, 7, 8, 9]
当index为负数时,向索引位置元素前插入数据
ls = [_ for _ in range(10)]
ls
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ls.insert(-1,"a")
ls
[0, 1, 2, 3, 4, 5, 6, 7, 8,'a',9]






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论