开班前自学--python list
作者:互联网
列表:容器型数据类型
1 读取List
1.1按索引
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] print(l1[0]) # output: front print(l1[2][1]) # output: cloudpark
1.2 按切片
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] print(l1[1:3]) # output: ['ove', ['library', 'cloudpark', 'CC+']]
2 list 的增
2.1 insert
list.insert(索引,'添加内容')
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1.insert(1,'A MAN CALLED') print(l1) # ['front', 'A MAN CALLED', 'ove', ['library', 'cloudpark', 'CC+'], '2019.03.01', '09:06']
2.2 append 追加
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1.append('Friday') l1.append('cloudy') print(l1) # ['front', 'ove', ['library', 'cloudpark', 'CC+'], '2019.03.01', '09:06', 'Friday', 'cloudy']
用append添加人名单
name_list = ['huawei','iphone','xiaomi','meizu'] while True: newname = input('请输入名称:按Q或q退出').strip().lower() # 后面加上stripe和lower,去除空格,转换大小写 if newname == 'q':break # 添加中途退出命令 name_list.append(newname) print('你已经成功添加添加了% s' %newname) # 此处要用格式化输出 print(name_list) # input: OPPO VIVO Q output:['huawei', 'iphone', 'xiaomi', 'meizu', 'oppo', 'vivo']
2.3 extend 分解成最小元素添加
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1.extend('云谷图书馆') print(l1) # ['front', 'ove', ['library', 'cloudpark', 'CC+'], '2019.03.01', '09:06', '云', '谷', '图', '书', '馆']
3 list 删
3.1 pop——用索引删除后
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1.pop(0) # 删除'fornt' print(l1.pop(0)) # 删除'ove',并返回删除值 print(l1) # ['library', 'cloudpark', 'CC+'], '2019.03.01', '09:06']
增删改查,只有pop才有返回,其它返回会print:None
3.2 remove——按元素删除
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1.remove('2019.03.01') print(l1) # ['front', 'ove', ['library', 'cloudpark', 'CC+'], '09:06']
3.3 clear ——清空列表
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1.clear() # 只清空不删除 print(l1) # []
3.4 del——删除列表
3.4.1 在内存内删除
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] del l1 print(l1) # 报错,无法找到l1列表
3.4.2 按索引删除
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] del l1[2] print(l1) # ['front', 'ove', '2019.03.01', '09:06']
3.4.3 按切片删除
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] del l1[0:2] print(l1) #[['library', 'cloudpark', 'CC+'], '2019.03.01', '09:06']
4 list 改
4.1 用索引更改
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1[0] = 'shenzhen' print(l1) # ['shenzhen', 'ove', ['library', 'cloudpark', 'CC+'], '2019.03.01', '09:06']
4.2 用切片更改
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1[0:1] = 'shenzhen','longgang' # 切片删除数量不一定要和增加一样,会直接删除并添加 print(l1) # ['shenzhen', 'longgang', 'ove', ['library', 'cloudpark', 'CC+'], '2019.03.01', '09:06']
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] l1[0:1] = 'shenzhen' # 改的内容为一个字符串时,会拆分再添加 print(l1) # ['s', 'h', 'e', 'n', 'z', 'h', 'e', 'n', 'ove', ['library', 'cloudpark', 'CC+'], '2019.03.01', '09:06']
5 list 查
for
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] for i in l1: print(i) # 分行列出列表内内容
6 list 其它操作
6.1 len 列表元素数量
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] cou = len(l1) print(cou) # 5
6.2 count 元素在列表中的数量
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06','ove'] print(l1.count('ove')) # 2
6.3 index 通过元素找索引
l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] print(l1.index('ove',1,4)) # 1 (可以切片查询)
6.4 sort 排序
l1 = [1,4,6,8,4,3,9,7,] l1.sort() # 列表中元素只能时int,否则报错 print(l1) # [1, 3, 4, 4, 6, 7, 8, 9]
l1 = [1,4,6,8,4,3,9,7,] l1.sort(reverse = True) # 倒序 print(l1) # [9, 8, 7, 6, 4, 4, 3, 1]
6.5 reverse
l1 = [1,4,6,8,4,3,9,7,] l1.reverse() # 反序 print(l1) # [7, 9, 3, 4, 8, 6, 4, 1]
7 list 嵌套
7.1 将list中元素大写
# 将'front'变为全部大写。 l1 = ['front','ove',['library',56,'cloudpark','CC+','16',],'2019.03.01','09:06'] l1[0] = l1[0].upper() print(l1) # ['FRONT', 'ove', ['library', 56, 'cloudpark', 'CC+', '16'], '2019.03.01', '09:06']
7.2 列表嵌套修改
# 将'cloudpark'变为首字母大写,并放回原处 l1 = ['front','ove',['library',56,' cloudpark','CC+','16',],'2019.03.01','09:06'] l1[2][2] = l1[2][2].strip().capitalize() print(l1) # ['front', 'ove', ['library', 56, 'Cloudpark', 'CC+', '16'], '2019.03.01', '09:06']
7.3 嵌套int变str
# 将56加10并变为字符串'65' l1 = ['front','ove',['library',56,' cloudpark','CC+','16',],'2019.03.01','09:06'] l1[2][1] = str(l1[2][1] + 10) print(l1) # ['front', 'ove', ['library', '66', ' cloudpark', 'CC+', '16'], '2019.03.01', '09:06']
7.4 嵌套内str变int
# '16'变成 10 l1 = ['front','ove',['library',56,' cloudpark','CC+','16',],'2019.03.01','09:06'] l1[2][-1] = int(l1[2][-1]) - 6 print(l1) # ['front', 'ove', ['library', 56, ' cloudpark', 'CC+', 10], '2019.03.01', '09:06']
标签:01,06,python,ove,list,library,开班,l1,cloudpark 来源: https://www.cnblogs.com/yingpeng/p/10455523.html