转自:微点阅读(www.weidianyuedu.com)微点阅读 - 范文大全 - 免费学习网站
列表
列表的使用
names=["ZhangSan","Guyang","Xianghuaqiang","Yuanbiao"];
print(names);
运行结果:
打印列表、["ZhangSan", "Guyang", "Xianghuaqiang", "Yuanbiao"]
1、取列表中的第一个值
print(names[0]);列表的小标是从0开始的,所以下表应该是0。
1、ZhangSan
2、取列表中第二个和第三个值
print(names[1:3]);这个称之为切片,将一段数据从中取出。顾头不顾尾
2、["Guyang", "Xianghuaqiang"]
3、取列表中最后一个值
print(names[3]);最后一个名字的下表就是3
3、Yuanbiao
4、在不了解列表长度的情况下如何取出最后一个值?
print(names[-1]);﹣1就是取某个列表的最后一个值
4、Yuanbiao
5、如何取出最后两个值?
print(names[-2:]);因为列表的取值是自左至右的,所以要先写出最左侧值的下表值
5、["Xianghuaqiang", "Yuanbiao"]
6、取列表中的前三个值
print(names[0:3]);
print(names[:3]);如果前面是0的话,可以忽略,效果与上面相同
6、["ZhangSan", "Guyang", "Xianghuaqiang"]
6、["ZhangSan", "Guyang", "Xianghuaqiang"]
7、在列表中追加一个值
names.append("Liuliu")#往列表中追加值,默认在最后
["ZhangSan", "Guyang", "Xianghuaqiang", "Yuanbiao", "Liuliu"]
8、在列表中插入一个值
names.insert(1,"Liumm")#在列表中插入一个值(第一个参数表示下标值,第二个参数是想插入的值得名称),每次只能插入一个值,不能批量插入
["ZhangSan", "Liumm", "Guyang", "Xianghuaqiang", "Yuanbiao"]
9、修改列表中的某一个值
names[2]="Maso"#修改列表中的某一个值,将列表中的guyang替换为Maso
["ZhangSan", "Guyang", "Maso", "Yuanbiao"]
10、在列表中删除一个值,有三种方法
names.remove("Liumm")#删除列表中的某一个值,直接写出名称
del names[1]#删除列表中的某一个值得第二种方法,写出下标值
names.pop()#如果不加下标值,默认删除列表中的最后一个值
["ZhangSan", "Guyang", "Xianghuaqiang", "Yuanbiao", "Liuliu"]
11、在列表中查找出某一个值的位置
print(names.index("Yuanbiao"))#找出列表中某一个值的位置
4
["ZhangSan", "Liumm", "Guyang", "Xianghuaqiang", "Yuanbiao", "Liuliu"]
12、将查找出的列表值进行输出
print(names[names.index("Yuanbiao")])#打印出列表中某一个值的名称
4
Yuanbiao
["ZhangSan", "Liumm", "Guyang", "Xianghuaqiang", "Yuanbiao", "Liuliu"]
13、统计在一个列表中的重复值的数量
print(names.count("Guyang"))#统计重名值的数量
["ZhangSan", "Liumm", "Guyang", "Xianghuaqiang", "Yuanbiao", "Guyang", "Liuliu"]
2
14、清空列表
names.clear()
print(names);
[]
15、列表翻转,逆序排列
names.reverse()#将列表的顺序进行逆序排列
print(names);
["ZhangSan", "Liumm", "Guyang", "Xianghuaqiang", "Yuanbiao", "Guyang", "Liuliu"]
["Liuliu", "Guyang", "Yuanbiao", "Xianghuaqiang", "Guyang", "Liumm", "ZhangSan"]
16、sort排序
names.sort()
print(names);
["ZhangSan", "Liumm", "Guyang", "Xianghuaqiang", "Yuanbiao", "Guyang", "Liuliu"]
["Guyang", "Guyang", "Liuliu", "Liumm", "Xianghuaqiang", "Yuanbiao", "ZhangSan"]
优先级为特殊符号、数字、大写字母、小写字母
["#@ZhangSan", "3Guyang", "Guyang", "Liuliu", "Liumm", "Yuanbiao", "xXianghuaqiang"]
[Finished in 0.2s]
17、extend,扩展列表,将列表进行合并,合并后原有列表依然存在
增加一个“name2”name2=[1,2,3,4]
names.extend(name2)#将name2与names列表进行合并
["#@ZhangSan", "Liumm", "3Guyang", "xXianghuaqiang", "Yuanbiao", "Guyang", "Liuliu"]
["#@ZhangSan", "Liumm", "3Guyang", "xXianghuaqiang", "Yuanbiao", "Guyang", "Liuliu", 1, 2, 3, 4] [1, 2, 3, 4]
[Finished in 0.2s]
del name2#合并后将已有的列表进行删除
["#@ZhangSan", "Liumm", "3Guyang", "xXianghuaqiang", "Yuanbiao", "Guyang", "Liuliu"]
["#@ZhangSan", "Liumm", "3Guyang", "xXianghuaqiang", "Yuanbiao", "Guyang", "Liuliu", 1, 2, 3, 4]
[Finished in 0.1s]
18、copy,列表复制
names=["#@ZhangSan","3Guyang","xXianghuaqiang","Yuanbiao","Guyang"];
name2=names.copy()
print(names);
print(name2);
["#@ZhangSan", "3Guyang", "xXianghuaqiang", "Yuanbiao", "Guyang"]
["#@ZhangSan", "3Guyang", "xXianghuaqiang", "Yuanbiao", "Guyang"]
[Finished in 0.2s]
如果在一个列表内插入了一个列表,这个列表内的值是不会被copy的,只是copy的它的内存值。
names=["#@ZhangSan","3Guyang","xXianghuaqiang",["ab","12"],"Yuanbiao","Guyang"];
names[2]="向华强"
names[3][0]="AB"
print(names);
print(name2);
["#@ZhangSan", "3Guyang", "向华强", ["AB", "12"], "Yuanbiao", "Guyang"]
["#@ZhangSan", "3Guyang", "xXianghuaqiang", ["AB", "12"], "Yuanbiao", "Guyang"]
[Finished in 0.1s]
19、如何完整克隆一份列表
import copy
name2=copy.deepcopy(names)
print(names);
print(name2);
["#@ZhangSan", "3Guyang", "xXianghuaqiang", ["ab", "12"], "Yuanbiao", "Guyang"]
["#@ZhangSan", "3Guyang", "xXianghuaqiang", ["ab", "12"], "Yuanbiao", "Guyang"]
[Finished in 0.2s]
20、步长切片
names=["#@ZhangSan","3Guyang","xXianghuaqiang",["ab","12"],"Yuanbiao","Guyang"];
print(names[0:-1:2])#0代表第一个值,-1代表最后一个值,步长值加2
print(names[::2])#第一个值和最后一个值的数字可以省略
运行结果相同
["#@ZhangSan", "xXianghuaqiang", "Yuanbiao"]
["#@ZhangSan", "xXianghuaqiang", "Yuanbiao"]
感谢阅读
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)