python从入门到放弃-day05-list(列表的增删改查)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python从入门到放弃-day05-list(列表的增删改查)相关的知识,希望对你有一定的参考价值。

#   作者 : liuxing
#   日期 : 2017-12-25
#   list用法 增删改查

names=["liuxing","wangnian","sunkang","xiaohang","zhujian","xuxian"]
#----------- 查-------------
print("names->",names)
print("names[1]->",names[1])
print("names[1:]->",names[1:])
print("names[:-1]->",names[:-1])
print("names[1:-1]->",names[1:-1])
print("names[1:-1:2]->",names[1:-1:2])
print("names[1::2]->",names[1::2])
print("names[::2]->",names[::2])
print("names[1::-1]->",names[1::-1])

#------------增---------------
names.append("ligang")   #添加到列表尾部
print("append->",names)

names.insert(1,"wangqiang") #添加到任意位置
print("insert->",names)

#------------改---------------
names[0]="xiongda"
names[1:3]=["baoqiang","jiangwen"]

#------------删---------------
names.remove("sunkang")
print("remove->",names)

name=names.pop(1)  #pop可以有返回值
print("pop->",names)

del names[2]
print("del->",names)
结果:
names-> [liuxing, wangnian, sunkang, xiaohang, zhujian, xuxian]
names[1]-> wangnian
names[1:]-> [wangnian, sunkang, xiaohang, zhujian, xuxian]
names[:-1]-> [liuxing, wangnian, sunkang, xiaohang, zhujian]
names[1:-1]-> [wangnian, sunkang, xiaohang, zhujian]
names[1:-1:2]-> [wangnian, xiaohang]
names[1::2]-> [wangnian, xiaohang, xuxian]
names[::2]-> [liuxing, sunkang, zhujian]
names[1::-1]-> [wangnian, liuxing]
append-> [liuxing, wangnian, sunkang, xiaohang, zhujian, xuxian, ligang]
insert-> [liuxing, wangqiang, wangnian, sunkang, xiaohang, zhujian, xuxian, ligang]
remove-> [xiongda, baoqiang, jiangwen, xiaohang, zhujian, xuxian, ligang]
pop-> [xiongda, jiangwen, xiaohang, zhujian, xuxian, ligang]
del-> [xiongda, jiangwen, zhujian, xuxian, ligang]

 

以上是关于python从入门到放弃-day05-list(列表的增删改查)的主要内容,如果未能解决你的问题,请参考以下文章

python从入门到放弃-day05-格式化输出购物车

python从入门到放弃-day05-用户登录(for , if ,break)

pytorch-geometric 从入门到不放弃 day3

python从入门到放弃(持续更新)

Python调用Zabbix api之从入门到放弃——从本节开始放弃

python从入门到放弃QAQ