读取列表
列表:由一系列按特定顺序排列的元素组合。Python中使用“[]”表示列表
bicycles = ['trek','cannondale','redline','specialized']
#读取所有列表
print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
#读取指定列表
print(bicycles[0])
trek
#读取指定列表,指定单词首字母大写
print(bicycles[0].title())
Trek
列表操作
列表命名规则:name = []
列表下标:[0,1,2,3....] 或 [...-3,-2,-1]
names = ["zhangsan","lisi","wangwu","maliu"]
查询列表
#显示列表
print(names)
['zhangsan', 'lisi', 'wangwu', 'maliu']
#取下标“0”的值===切片
print(names[0])
zhangsan
#连续的正向取值,"1,2,3",取值规则顾头不顾尾,顺序左到右===切片
print(names[1:4])
['lisi', 'wangwu', 'maliu']
#连续的逆向取值,“-2,-1”===切片
print(names[-2:])
['wangwu', 'maliu']
#连续的逆向取值,“-2”顾头不顾尾===切片
print(names[-2:-1])
['wangwu']
#连续的逆向取值,“0,1,2”顾头不顾尾===切片
print(names[0:3])
print(names[:3])
['zhangsan', 'lisi', 'wangwu']
['zhangsan', 'lisi', 'wangwu']
#查询列表长度
print(len(names))
print(names)
4
['zhangsan', 'lisi', 'wangwu', 'maliu']
#查询参数的下标位
print(names.index("zhangsan"))
0
#统计参数个数
print(names.count("zhangsan"))
1
#反转列表
names.reverse()
print(names)
['maliu', 'wangwu', 'lisi', 'zhangsan']
#规则排序,特殊字符====数字===大写===小写
names.sort() print(names)
['lisi', 'maliu', 'wangwu', 'zhangsan']
#清空列表
print(names.clear())
增加列表
name = []
names = ["zhangsan","lisi","wangwu","maliu"]
# 追加参数
names.append("xiaoqi")
print(names)
['zhangsan', 'lisi', 'wangwu', 'maliu', 'xiaoqi']
# 指定位置增加参数
names.insert(1, "xiaoqi") print(names)
['zhangsan', 'xiaoqi', 'lisi', 'wangwu', 'maliu', 'xiaoqi']
# 列表扩展
names2 = ["123", "234"] names.extend(names2) print(names)
['zhangsan', 'xiaoqi', 'lisi', 'wangwu', 'maliu', 'xiaoqi', '123', '234']
删除列表
name = []
names = ["zhangsan","lisi","wangwu","maliu"]
print(names)
['zhangsan', 'lisi', 'wangwu', 'maliu']
# 替换指定参数
names[2] = "xiaoqi"
print(names)
['zhangsan', 'lisi', 'xiaoqi', 'maliu']
# 删除指定参数
names.remove("zhangsan")
print(names)
['lisi', 'xiaoqi', 'maliu']
# 默认删除最后一个,输入下标删除下标位
names.pop()
print(names)
['lisi', 'xiaoqi']
# 删除变量
names2 = ["123", "234"]
del names2
复制列表
#浅copy,仅复制第一层列表
names = ["zhangsan","lisi","wangwu","maliu"]
name2 = names.copy()
#完全复制一层列表
print(names)
['zhangsan', 'lisi', 'wangwu', 'maliu']
names[0] = "lili"
print(names)
['lili', 'lisi', 'wangwu', 'maliu']
print(name2)
['zhangsan', 'lisi', 'wangwu', 'maliu']
#浅copy,对第二层列表完全读取
names = ["zhangsan","lisi",["zhangsan","lisi"],"wangwu","maliu"]
name2 = names.copy()
#完全复制第一层列表
names [2][1] = "LLLL"
print(names)
["zhangsan","lisi",["zhangsan","LLLL"],"wangwu","maliu"]
print(name2)
["zhangsan","lisi",["zhangsan","LLLL"],"wangwu","maliu"]
copy模块
#深copy,完全克隆
import copy
names = ["zhangsan","lisi",["zhangsan","lisi"],"wangwu","maliu"]
name2 = copy.deepcopy(names)
#完全复制一层列表
names [2][1] = "LLLL"
print(names)
["zhangsan","lisi",["zhangsan","LLLL"],"wangwu","maliu"]
print(name2)
["zhangsan","lisi",["zhangsan","lisi"],"wangwu","maliu"]
列表循环
names = ["zhangsan", "lisi", "wangwu", "maliu","zhaoliu"]
# 步长切片
for i in names: print(i) print(names[0:-1:2]) print(names[::2])
zhangsan
lisi
wangwu
maliu
zhaoliu
['zhangsan', 'wangwu']
['zhangsan', 'wangwu', 'zhaoliu']
创建数字列表
#创建2-11,步长为2的数字列表“numb”
numb = list(range (2,11,2))
print(numb)
[2, 4, 6, 8, 10]
数字列表简单计算
numb = list(range (0,10))
print(numb)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#列表取最小值
print(min(numb))
0
#列表取最大值
print(max(numb))
9
#列表求和
print(sum(numb))
45