本节内容
- 列表、元组操作
- 字符串操作
- 字典操作
1、列表、元组操作
列表
列表是经常操作的数据形式之一,可以对列表对数据进行简单的增删改查等操作。
定义列表
1 names = ['ZhangYang','Guyun','Xiangpeng','XuLiangchen']
通过下标访问列表中的元素,下标从0开始计数
1 >>> names[0] 2 'ZhangYang' 3 >>> names[2] 4 'Xiangpeng' 5 >>> names[-1] 6 'XuLiangchen' 7 >>> names[-2] #倒序取 8 'Xiangpeng'
切片,取多个元素
1 >>> names = ['ZhangYang','Guyun','Xiangpeng','Xmy','XuLiangchen'] 2 >>> names[1:3] 3 ['Guyun', 'Xiangpeng'] 4 >>> names[1:-1] 5 ['Guyun', 'Xiangpeng', 'Xmy'] 6 >>> names[:3] #等同于names[0:3] 7 ['ZhangYang', 'Guyun', 'Xiangpeng'] 8 >>> names[3:] 9 ['Xmy', 'XuLiangchen'] 10 >>> names[0::2] #后面的2是代表,每隔一个元素,就取一个,等同于names[::2] 11 ['ZhangYang', 'Xiangpeng', 'XuLiangchen']
追加
1 >>> names 2 ['ZhangYang', 'Guyun', 'Xiangpeng', 'Xmy', 'XuLiangchen'] 3 >>> names.append('LeiHaidong') 4 >>> names 5 ['ZhangYang', 'Guyun', 'Xiangpeng', 'Xmy', 'XuLiangchen', 'LeiHaidong']
插入
1 >>> names 2 ['ZhangYang', 'Guyun', 'Xiangpeng', 'Xmy', 'XuLiangchen', 'LeiHaidong'] 3 >>> names.insert(2,'flashman') #插入到位置2 4 >>> names 5 ['ZhangYang', 'Guyun', 'flashman', 'Xiangpeng', 'Xmy', 'XuLiangchen', 'LeiHaidong']
修改
1 >>> names 2 ['ZhangYang', 'Guyun', 'flashman', 'Xiangpeng', 'Xmy', 'XuLiangchen', 'LeiHaidong'] 3 >>> names[2] = 'oldman' 4 >>> names 5 ['ZhangYang', 'Guyun', 'oldman', 'Xiangpeng', 'Xmy', 'XuLiangchen', 'LeiHaidong']
删除
1 >>> names 2 ['ZhangYang', 'Guyun', 'flashman', 'Xiangpeng', 'Xmy', 'XuLiangchen', 'LeiHaidong'] 3 >>> names[2] = 'oldman' 4 >>> names 5 ['ZhangYang', 'Guyun', 'oldman', 'Xiangpeng', 'Xmy', 'XuLiangchen', 'LeiHaidong'] 6 >>> del names[2] 7 >>> names 8 ['ZhangYang', 'Guyun', 'Xiangpeng', 'Xmy', 'XuLiangchen', 'LeiHaidong'] 9 >>> names.remove('Xmy') #指定删除元素 10 >>> names 11 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', 'LeiHaidong'] 12 >>> names.pop() #删除最后一个值 13 'LeiHaidong' 14 >>> names 15 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen']
扩展
1 >>> names 2 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen'] 3 >>> names2 = [1,2,3,4] 4 >>> names.extend(names2) 5 >>> names 6 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', 1, 2, 3, 4]
拷贝
1 >>> names 2 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', 1, 2, 3, 4] 3 >>> names_copy = names.copy() #浅拷贝 4 >>> names_copy 5 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', 1, 2, 3, 4] 6 >>> 7 >>> names.insert(4,['Xmy','Zhb']) 8 >>> names 9 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', ['Xmy', 'Zhb'], 1, 2, 3, 4] 10 >>> names_copy = copy.copy(names) #浅拷贝 11 >>> names_copy2 = copy.deepcopy(names) #深拷贝 12 >>> names_copy 13 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', ['Xmy', 'Zhb'], 1, 2, 3, 4] 14 >>> names_copy2 15 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', ['Xmy', 'Zhb'], 1, 2, 3, 4] 16 >>> names[2]='向鹏' 17 >>> names[4][0] = 'xumengyuan' 18 >>> names 19 ['ZhangYang', 'Guyun', '向鹏', 'XuLiangchen', ['xumengyuan', 'Zhb'], 1, 2, 3, 4] 20 >>> names_copy 21 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', ['xumengyuan', 'Zhb'], 1, 2, 3, 4] 22 >>> names_copy2 23 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangchen', ['Xmy', 'Zhb'], 1, 2, 3, 4]
统计
1 >>> names 2 ['ZhangYang', 'Guyun', '向鹏', 'XuLiangchen', ['xumengyuan', 'Zhb'], 'Guyun',1, 2, 3, 4] 3 >>> names.count('xumengyuan') 4 0 5 >>> names.count(['xumengyuan', 'Zhb']) 6 1 7 >>> names.count('Guyun') 8 2
排序&反转
1 >>> names 2 ['ZhangYang', 'Guyun', '向鹏', 'XuLiangchen', 1, 2, 3, 4] 3 >>> names.sort() #排序 4 Traceback (most recent call last): 5 File "<input>", line 1, in <module> 6 TypeError: unorderable types: list() < str() #3.0里不同数据类型不能放在一起排序了 7 >>> names[-4:] = '1','2','3','4' 8 >>> names 9 ['ZhangYang', 'Guyun', '向鹏', 'XuLiangchen', '1', '2', '3', '4'] 10 >>> names.sort() 11 >>> names 12 ['1', '2', '3', '4', 'Guyun', 'XuLiangchen', 'ZhangYang', '向鹏'] 13 >>> names.reverse() #反转 14 >>> names 15 ['向鹏', 'ZhangYang', 'XuLiangchen', 'Guyun', '4', '3', '2', '1']
获取下标
1 >>> names 2 ['向鹏', 'ZhangYang', 'XuLiangchen', 'Guyun', '4', '3', '2', '1'] 3 >>> names.index('3') 4 5
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
1 names = ('Alex','Jack')
它只有2个方法,一个是count,一个是index,完毕。
2、字符串操作
特性:不可修改
定义字符串
1 name = 'My name is {name} and I am {year} old' 2 name2 = 'My \tname is {name} and I am {year} old'
字符串相关函数
1 >>> name.capitalize() #首字母大写 2 'My name is {name} and i am {year} old' 3 >>> name.count('m') 统计m出现的次数 4 3 5 >>> name.center(50,'-') #输出如下 6 '------My name is {name} and I am {year} old-------' 7 >>> name.endswith('my') #判断字符串是否以my结尾 8 False 9 >>> name.expandtabs(tabsize=30) 10 'My name is {name} and I am {year} old' 11 >>> name2.expandtabs(tabsize=30) #将\t转换成多长的空格 12 'My name is {name} and I am {year} old' 13 >>> name.find('name') #查找那么并返回其索引,找不到则返回-1 14 3 15 >>> name[name.find('name'):9] #可以用find做切片 16 'name i' 17 18 #format 19 >>> msg = "my name is {}, and age is {}" 20 >>> msg.format("Xmy",23) 21 'My name is Xmy and I am 23' 22 >>> msg = "my name is {1}, and age is {0}" 23 msg.format("Xmy",23) 24 'My name is 23 and I am Xmy' 25 >>> msg = "my name is {name}, and age is {age}" 26 >>> msg.format(age=23,name="Xmy") 27 'my name is Xmy, and age is 23' 28 #format_map 29 >>> name.format_map({'name':'Xmy','year':12}) 30 'my name is Xmy, and age is 23' 31 32 33 >>> 'ab23'.isalnum() 34 True 35 >>> 'abA'.isalpha() 36 True 37 >>> '1A'.isdecimal() 38 False 39 >>> '1A'.isdigit() 40 False 41 >>> 'a1A'.isidentifier() #判断是不是一个合法的标识符 42 True 43 >>> '33a'.isnumeric() 44 False 45 >>> ' '.isspace() 46 True 47 >>> 'My Name Is'.istitle() 48 True 49 >>> 'My Name Is'.isprintable() 50 True 51 >>> 'My Name Is'.isupper() 52 False 53 >>> '+'.join(['1','2','3','4']) 54 '1+2+3+4' 55 >>> name.ljust(50,'*') 56 'My name is {name} and I am {year} old*************' 57 >>> name.rjust(50,'*') 58 '*************My name is {name} and I am {year} old' 59 >>> name.lower() 60 'my name is {name} and i am {year} old' 61 >>> name.upper() 62 'MY NAME IS {NAME} AND I AM {YEAR} OLD' 63 >>> '\nAlex'.lstrip() 64 'Alex' 65 >>> 'Alex\n'.rstrip() 66 'Alex' 67 >>> ' Alex\n'.strip() 68 'Alex' 69 70 #maketrans 71 >>> p = str.maketrans('abcdef','123456') 72 >>> 'alex li'.translate(p) 73 '1l5x li' 74 75 >>> 'Alex li'.replace('l','L',1) 76 'ALex li' 77 >>> 'alex li'.rfind('l') 78 5 79 >>> 'alex li'.split('l') 80 ['a', 'ex ', 'i'] 81 >>> 'alex\nli'.splitlines() 82 ['alex', 'li'] 83 >>> 'Alex Li'.swapcase() #大小写互换 84 'aLEX lI' 85 >>> 'alex li'.title() 86 'Alex Li' 87 >>> 'alex'.zfill(50) 88 '0000000000000000000000000000000000000000000000alex'
3、字典操作
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
语法:
info = { 'stu1101':'TengLan Wu', 'stu1102':'LongZe Luola', 'stu1103':'XiaoZe Maliya', }
字典的特性:
- dict是无序的
- key必须是唯一的,so 天生去重
增加
1 >>> info['stu1104'] = 'CangJing Kong' 2 >>> info 3 {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1104': 'CangJing Kong'}
修改
1 >>> info['stu1102'] = '武藤兰' 2 >>> info 3 {'stu1101': 'TengLan Wu', 'stu1102': '武藤兰', 'stu1103': 'XiaoZe Maliya'}
删除
1 >>> info 2 {'stu1101': 'TengLan Wu', 'stu1102': '武藤兰', 'stu1103': 'XiaoZe Maliya'} 3 >>> info.pop('stu1101') 4 'TengLan Wu' 5 >>> info 6 {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} 7 >>> del info['stu1102'] 8 >>> info 9 {'stu1103': 'XiaoZe Maliya'} 10 >>> 11 >>> 12 >>>info 13 {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} 14 >>> info.popitem() 15 ('stu1103', 'XiaoZe Maliya') 16 >>> info 17 {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola'}
查找
1 >>> info 2 {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} 3 >>> 'stu1103' in info 4 True 5 >>> info.get('stu1103') 6 'XiaoZe Maliya' 7 >>> info['stu1103'] 8 'XiaoZe Maliya' 9 >>> info['stu1105'] #如果一个key不存在,就报错,get不会,不存在只返回None 10 Traceback (most recent call last): 11 File "<input>", line 1, in <module> 12 KeyError: 'stu1105'
多级字典嵌套及操作
1 >>> av_catalog = { 2 ... "欧美":{ 3 ... "www.youporn.com": ["很多免费的,世界最大的","质量一般"], 4 ... "www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"], 5 ... "letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"], 6 ... "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"] 7 ... }, 8 ... "日韩":{ 9 ... "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"] 10 ... }, 11 ... "大陆":{ 12 ... "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"] 13 ... } 14 ... } 15 >>> av_catalog["大陆"]["1024"][1] += ",可以用爬虫爬下来" 16 >>> print(av_catalog["大陆"]["1024"]) 17 ['全部免费,真好,好人一生平安', '服务器在国外,慢,可以用爬虫爬下来']
其他
1 #values 2 >>> info.values() 3 dict_values(['TengLan Wu', 'LongZe Luola', 'XiaoZe Maliya']) 4 5 #keys 6 >>> info.keys() 7 dict_keys(['stu1101', 'stu1102', 'stu1103']) 8 9 #setdefault 10 >>> info.setdefault('stu1101','SDASGA') 11 'TengLan Wu' 12 >>> info 13 {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} 14 >>> info.setdefault('stu1105','SDASGA') 15 'SDASGA' 16 >>> info 17 {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1105': 'SDASGA'} 18 19 #update 20 >>> info 21 {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1105': 'SDASGA'} 22 >>> b = { 23 ... 'stu1101':'Alex Li', 24 ... 1:3, 25 ... 2:5 26 ... } 27 >>> info.update(b) 28 >>> info 29 {'stu1101': 'Alex Li', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1105': 'SDASGA', 1: 3, 2: 5} 30 31 #items 32 >>> info.items() 33 dict_items([('stu1101', 'Alex Li'), ('stu1102', 'LongZe Luola'), ('stu1103', 'XiaoZe Maliya'), ('stu1105', 'SDASGA'), (1, 3), (2, 5)]) 34 35 #初始化一个字典 36 >>> c = dict.fromkeys([1,2,3],[1,{'name':'Xmy'},444]) 37 >>> c 38 {1: [1, {'name': 'Xmy'}, 444], 2: [1, {'name': 'Xmy'}, 444], 3: [1, {'name': 'Xmy'}, 444]}
循环dict
1 #method_1 2 for i in info: # 更高效 3 print(i,info[i]) 4 5 #method_2 6 for k,v in info.items(): #会先把dict转成list,数据里大时莫用 7 print(k,v)