概述
列表是Python里面最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作。
1、列表的定义
names = ["Liu","Zhang","Mao"]
print(names)
#打印结果
#['Liu', 'Zhang', 'Mao']
2、通过下标取列表中的元素,其中,下标是从0开始计数的
names = ["Liu","Zhang","Mao"]
print(names[0],names[2])
print(names[-1],names[-2])
#倒着取
#打印结果
#['Liu', 'Zhang', 'Mao']
#Liu Mao
#Mao Zhang
3、从列表中取多个元素:切片(顺序:从左向右)
names = ["Liu","Zhang","Mao","Sun","Wang","Song"]
print(names[1:4])
#打印结果
#['Zhang', 'Mao', 'Sun']
#特点:顾头不顾尾(左闭右开)
names = ["Liu","Zhang","Mao","Sun","Wang","Song"]
print(names[-3:])
#如果想取最后一个,必须不能写-1,只能这么写
#结果:['Sun', 'Wang', 'Song']
print(names[:3])
#如果是从头开始取,0可以忽略
#结果:['Liu', 'Zhang', 'Mao']
4、在列表后边追加新的字符串:调用.append("新添加的内容")
names = ["Liu","Zhang","Mao","Sun","Wang","Song"]
print(names)
names.append("Zhao")
print(names)
#结果:
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song']
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Zhao']
5、在列表中插入新的字符串:调用.insert(要插入的位置下标,要插入的内容)
names = ["Liu","Zhang","Mao","Sun","Wang","Song"]
print(names)
names.insert(1,"Chen")
print(names)
#结果:
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song']
#['Liu', 'Chen', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song']
6、在列表中修改某个位置的字符串:直接修改该位置的内容
names = ["Liu","Zhang","Mao","Sun","Wang","Song"]
print(names)
names[2] = "Li"
print(names)
#运行结果
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song']
#['Liu', 'Zhang', 'Li', 'Sun', 'Wang', 'Song']
7、
在列表中删除某个字符串:直接删除del;删除指定的字符串:remove;
删除最后一个元素(不添加下标时,添加下标与del等价):pop
names = ["Liu","Zhang","Mao","Sun","Wang","Song"]
print(names)
del names[1]
print(names)
names.remove("Mao")
print(names)
names.pop()
print(names)
#输出结果:
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song']
#['Liu', 'Mao', 'Sun', 'Wang', 'Song']
#['Liu', 'Sun', 'Wang', 'Song']
#['Liu', 'Sun', 'Wang']
8、
在列表中查找某个元素所在的位置:调用.index("要查找的元素名")
names = ["Liu","Zhang","Mao","Sun","Wang","Song"]
print(names.index("Mao"))
print(names[names.index("Mao")])
#运行结果:
#2
#Mao
9、在列表中统计同一个元素出现的次数:调用.count("要统计的元素名")
names = ["Liu","Zhang","Mao","Sun","Wang","Song","Mao","Mao"]
print(names.count("Mao"))
#运行结果
#3
10、清空列表中元素:调用.clear()
names = ["Liu","Zhang","Mao","Sun","Wang","Song","Mao","Mao"]
names.clear()
print(names)
#运行结果:
#[]
11、翻转列表中的元素的位置:调用.reverse()
names = ["Liu","Zhang","Mao","Sun","Wang","Song","Mao","Mao"]
print(names)
names.reverse()
print(names)
#运行结果:
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Mao', 'Mao']
#['Mao', 'Mao', 'Song', 'Wang', 'Sun', 'Mao', 'Zhang', 'Liu']
12、排序列表中的元素(安装字母的起始顺序):调用.sort()
names = ["Liu","Zhang","Mao","Sun","Wang","Song","Mao","Mao"]
print(names)
names.sort()
print(names)
#运行结果:
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Mao', 'Mao']
#['Liu', 'Mao', 'Mao', 'Mao', 'Song', 'Sun', 'Wang', 'Zhang']
13、在列表中扩展某些新元素:调用.extend(扩展的内容)
names = ["Liu","Zhang","Mao","Sun","Wang","Song","Mao","Mao"]
print(names)
names2 = ["1san","Shi","Meng"]
names.extend(names2)
print(names)
print(names2)
#运行结果:
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Mao', 'Mao']
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Mao', 'Mao', '1san', 'Shi', 'Meng']
#['1san', 'Shi', 'Meng']
14、复制列表中的元素:调用.copy()。复制结束后再修改原来列表里面的元素,拷贝的内容不变。
names = ["Liu","Zhang","Mao","Sun","Wang","Song","Mao","Mao"]
names1 = names.copy()
print(names)
print(names1)
names[0] = "刘"
print(names)
print(names1)
#运行结果:
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Mao', 'Mao']
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Mao', 'Mao']
#['刘', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Mao', 'Mao']
#['Liu', 'Zhang', 'Mao', 'Sun', 'Wang', 'Song', 'Mao', 'Mao']
15、列表里面还可以嵌套新的列表(被嵌套的整体作为嵌套的一个下标)
names = ["Liu","Zhang","Mao","Sun",["Li","Tian"],"Wang","Song","Mao","Mao"]
print(names)
#运行结果:
#['Liu', 'Zhang', 'Mao', 'Sun', ['Li', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
(1)注:copy结合嵌套列表:copy只是复制第一层的元素,嵌套的第二层只是复制内存地址,不复制元素内容(浅复制)
names = ["Liu","Zhang","Mao","Sun",["Li","Tian"],"Wang","Song","Mao","Mao"]
names1 = names.copy()
print(names)
print(names1)
names[0] = "刘"
names[4][0] = "李"
print(names)
print(names1)
#运行结果:
#['Liu', 'Zhang', 'Mao', 'Sun', ['Li', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
#['Liu', 'Zhang', 'Mao', 'Sun', ['Li', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
#['刘', 'Zhang', 'Mao', 'Sun', ['李', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
#['Liu', 'Zhang', 'Mao', 'Sun', ['李', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
(2)引入独立的copy模块,调用copy模块下面的copy方法实现复制(与上面的浅复制是一样的打印效果)
import copy
names = ["Liu","Zhang","Mao","Sun",["Li","Tian"],"Wang","Song","Mao","Mao"]
names1 = copy.copy(names)
print(names)
print(names1)
names[0] = "刘"
names1[4][0] = "李"
print(names)
(3)要想完全独立复制,需调用copy模块下面的deepcopy方法,一般情况下不需要完全独立复制一个列表(少用deepcopy)。
import copy
names = ["Liu","Zhang","Mao","Sun",["Li","Tian"],"Wang","Song","Mao","Mao"]
names1 = copy.deepcopy(names)
print(names)
print(names1)
names[0] = "刘"
names[4][0] = "李"
print(names)
print(names1)
#运行结果:
#['Liu', 'Zhang', 'Mao', 'Sun', ['Li', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
#['Liu', 'Zhang', 'Mao', 'Sun', ['Li', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
#['刘', 'Zhang', 'Mao', 'Sun', ['李', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
#['Liu', 'Zhang', 'Mao', 'Sun', ['Li', 'Tian'], 'Wang', 'Song', 'Mao', 'Mao']
16、列表循环(for循环)
names = ["Liu","Zhang","Mao","Sun",["Li","Tian"],"Wang","Song"]
for i in names:
print(i)
#运行结果:
#Liu
#Zhang
#Mao
#Sun
#['Li', 'Tian']
#Wang
#Song
17、列表的切片(以步长是2为例)
names = ["Liu","Zhang","Mao","Sun",["Li","Tian"],"Wang","Song"]
print(names[0:-1:2])
#0和-1都可以去掉,即:print(names[::2])
#运行结果:#['Liu', 'Mao', ['Li', 'Tian']]
最后
以上就是自信凉面为你收集整理的Python3.5--列表(上)的全部内容,希望文章能够帮你解决Python3.5--列表(上)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复