Python数据类型的操作——列表、元组
一、列表的索引和切片
1 li = ['a','b','c',1,2,3] 2 print(li[0]) 3 print(li[2]) 4 print(li[-1]) 5 # a 6 # c 7 # 3 8 #切片 9 print(li[0:3]) 10 print(li[2:5]) 11 print(li[0:5:2]) 12 # ['a', 'b', 'c'] 13 # ['c', 1, 2] 14 # ['a', 'c', 2]
二、增
2.1、append(self, p_object) 添加
1 Demo = ['name','age','job'] 2 Demo.append('hobby') #在列表后面新增元素 3 print(Demo) 4 Demo.append([1,'hehe',True]) 5 print(Demo) #直接添加整个列表 6 # ['name', 'age', 'job', 'hobby'] 7 # ['name', 'age', 'job', 'hobby', [1, 'hehe', True]]
2.2、insert(self, index, p_object) 插入
1 Demo = ['name','age','job'] 2 Demo.insert(1,'kitty') #通过索引插入 3 print(Demo) 4 # ['name', 'kitty', 'age', 'job']
nsert()只能通过 索引 插入
2.3、extend(self, iterable)
在列表后面 可迭代 的添加
1 Demo = ['name','age','job'] 2 Demo.extend(' ') 3 print(Demo) 4 # ['name', 'age', 'job', ' '] 5 Demo = ['name','age','job'] 6 Demo.extend('a') 7 print(Demo) 8 # ['name', 'age', 'job', 'a'] 9 Demo = ['name','age','job'] 10 Demo.extend('abc') #可迭代的对象拆分遍历后添加 11 print(Demo) 12 # ['name', 'age', 'job', 'a', 'b', 'c'] 13 Demo = ['name','age','job'] 14 Demo.extend(['abc','b','c']) #可迭代的对象拆分(子序列)遍历后添加 15 print(Demo) 16 # ['name', 'age', 'job', 'abc', 'b', 'c']
三、删
3.1、pop(self, index=None)
通过索引删除
1 Demo = ['name','age','job'] 2 t = Demo.pop(1) #pop(索引) 3 print(Demo) 4 print(t) 5 # ['name', 'job'] 6 # age
3.2、remove(self, value)
直接删除看到的值
1 Demo = ['name','age','job'] 2 Demo.remove('age') 3 print(Demo) 4 # ['name', 'job']
3.3、clear(self)
清空列表
1 Demo = ['name','age','job'] 2 Demo.clear() 3 print(Demo) 4 # []
3.4、del
删除
1 Demo = ['name','age','job','wall_e','eva',1,2,3] 2 del Demo[2] #索引删除 3 print(Demo) 4 # ['name', 'age', 'wall_e', 'eva', 1, 2, 3] 5 Demo = ['name','age','job','wall_e','eva',1,2,3] 6 del Demo[2:6:2] #切片删除 7 print(Demo) 8 # ['name', 'age', 'wall_e', 1, 2, 3] 9 Demo = ['name','age','job','wall_e','eva',1,2,3] 10 del Demo #把列表删了,列表不存在了,报错 not defined 11 print(Demo) 12 # Traceback (most recent call last): 13 # File "D:/Demo/test.py", line 11, in <module> 14 # print(Demo) 15 # NameError: name 'Demo' is not defined
四、改
4.1 通过索引改
4.2 通过切片改 (添加可迭代的对象)
1 Demo = ['name','age','job','wall_e','eva',1,2,3] 2 Demo[2] = 'hobby' 3 print(Demo) 4 # ['name', 'age', 'hobby', 'wall_e', 'eva', 1, 2, 3] 5 Demo = ['name','age','job','wall_e','eva',1,2,3] 6 Demo[2:5:] = 'abcde' 7 print(Demo) 8 # ['name', 'age', 'a', 'b', 'c', 'd', 'e', 1, 2, 3]
五、查
通过索引查
通过切片查
通过for循环 列出查看
1 Demo = ['name','age','job','wall_e','eva',1,2,3] 2 print(Demo[2]) 3 # job 4 print(Demo[5:0:-2]) 5 # [1, 'wall_e', 'age'] 6 7 li = ['aefc','df',1,5,3] 8 for i in li: 9 print(i) 10 # aefc 11 # df 12 # 1 13 # 5 14 # 3
###枚举 enumerrate(self, iterable, start = 0)
iterable: str, list, tuple, dict, set
1 li = ['a','b','c','d'] 2 for i in enumerate(li): 3 print(i) 4 # (0, 'a') 5 # (1, 'b') 6 # (2, 'c') 7 # (3, 'd') 8 for i in enumerate(li,6) 9 (# (6, 'a') 10 # (7, 'b') 11 # (8, 'c') 12 # (9, 'd')
六、index()
通过值 取 索引
七、count 计数
1 Demo = ['name','age',['name','wall_e','name',1],2,'name'] 2 print(Demo.count('name')) 3 # 2 4 print(Demo.count('haha')) 5 # 0
八、排序
sort(self, key=None, reverse=False) 正序
sort(reverse=True) 倒序
1 Demo = [1,2,3,7,6,0,4,5] 2 Demo.sort() 3 print(Demo) 4 Demo = [1,2,3,7,6,0,4,5] 5 Demo.sort(reverse=True) 6 print(Demo) 7 # [0, 1, 2, 3, 4, 5, 6, 7] 8 # [7, 6, 5, 4, 3, 2, 1, 0]
九、reverse(self) 反转
1 Demo = [1,2,3,7,6,0,4,5] 2 Demo.reverse() 3 print(Demo) 4 # [5, 4, 0, 6, 7, 3, 2, 1]
### 将字符串转换成列表,split
将列表转换成字符串,join (列表的每个值都是字符串)
1 b = ['a','b','c'] 2 a = '_'.join(b) 3 print(a) 4 # a_b_c 5 6 a = 'Tom' 7 b = '5'.join(a) 8 print(b) 9 # T5o5m
十、列表的嵌套
1 Demo = ['name','age',['job','wall_e','eva',1],2,3] 2 Demo[1] = 'AGE' 3 print(Demo) 4 # ['name', 'AGE', ['job', 'wall_e', 'eva', 1], 2, 3] 5 Demo = ['name','age',['job','wall_e','eva',1],2,3] 6 Demo[1] = Demo[1].upper() 7 print(Demo) 8 # ['name', 'AGE', ['job', 'wall_e', 'eva', 1], 2, 3] 9 #***必须把新生成的AGE 替换掉原来的, (通过索引改) 10 Demo = ['name','age',['job','wall_e','eva',1],2,3] 11 Demo[2][2] = Demo[2][2].upper() 12 print(Demo) 13 # ['name', 'age', ['job', 'wall_e', 'EVA', 1], 2, 3] 14 15 #列表元素 首字母大写 16 Demo = ['name','age',['job','wall_e','eva',1],2,3] 17 Demo[0].capitalize() 18 print(Demo) 19 print(Demo[0].capitalize()) 20 # ['name', 'age', ['job', 'wall_e', 'eva', 1], 2, 3] 21 # Name 22 #不替换新的元素,原列表是不变的,即使列表是可变类型,因为字符串是不可变类型, 23 #对字符串的操作不会改变原字符串 24 Demo[0] = Demo[0].capitalize() 25 print(Demo) 26 # ['Name', 'age', ['job', 'wall_e', 'eva', 1], 2, 3]
十一、元组
只读,不可修改
元组不可修改,但是元组如果包含列表,列表的内容可以更改
1 a = (1,3,5) 2 a[1] = 9 #报错,tuple 对象不支持更改 3 print(a) 4 # Traceback (most recent call last): 5 # File "D:/Demo/test.py", line 2, in <module> 6 # a[1] = 9 7 # TypeError: 'tuple' object does not support item assignment 8 9 Demo = ('name','age',['wall_e','name',1],2) 10 Demo[2][1] = 'job' 11 print(Demo) 12 # ('name', 'age', ['wall_e', 'job', 1], 2)
十二、range()
包含指定范围
1 for i in range (1,10,2): #可加步长 2 print(i)