1、Python列表
列表是Python中最基本的数据结构,通过列表可以对数据实现最方便的存储、修改等操作,列表是有顺序的。
定义列表:
1 names = ['Vison','Tenglan','Eric']
通过下标访问列表中的元素,下标从0开始计数,下标取负数可以从列表结尾访问元素。
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 names = ['Vison','Tenglan','Eric'] 6 7 print('Name1:',names[0]) 8 print('Name3:',names[2]) 9 print('Name_end:',names[-1]) 10 print('Name2:',names[-2]) 11 12 13 # Name1: Vison 14 # Name3: Eric 15 # Name_end: Eric 16 # Name2: Tenglan
列表切片:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> names = ["Vison","Tenglan","Eric","Rain","Tom","Amy"] 6 >>> names[1:4] #取下标1至下标4之间的元素,包括1,不包括4 7 ['Tenglan', 'Eric', 'Rain'] 8 >>> names[1:-1] #取下标1至结尾之间的元素,包括1,不包括结尾 9 ['Tenglan', 'Eric', 'Rain', 'Tom'] 10 >>> names[:3] #如果是头开始取,0可以忽略 11 ['Vison', 'Tenglan', 'Eric'] 12 >>> names[3:] #如果想取最后一个,必须不能写-1,只能这么写 13 ['Rain', 'Tom', 'Amy'] 14 >>> names[3:-1] #这样-1就不会被包含了 15 ['Rain', 'Tom'] 16 >>> names[0::2] #后面的2代表每隔一个元素,取一下 17 ['Vison', 'Eric', 'Tom'] 18 >>> names[::2] #和上句效果一样 19 ['Vison', 'Eric', 'Tom']
列表追加:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> names = ["Vison","Tenglan","Eric","Rain","Tom","Amy"] 5 >>> names 6 ['Vison', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy'] 7 >>> names.append("我是新来的") #append方法用来在列表末尾加入新元素 8 >>> names 9 ['Vison', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的']
列表插入:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> names 6 ['Vison', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的'] 7 >>> names.insert(2,'强行从Tenglan后面插入') #insert方法用来在指定下标处插入新元素 8 >>> names 9 ['Vison', 'Tenglan', '强行从Tenglan后面插入', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的'] 10 >>> names.insert(5,'强行从Rain后面插入') 11 >>> names 12 ['Vison', 'Tenglan', '强行从Tenglan后面插入', 'Eric', 'Rain', '强行从Rain后面插入', 'Tom', 'Amy', '我是新来的']
列表修改:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> names 5 ['Vison', 'Tenglan', '强行从Tenglan后面插入', 'Eric', 'Rain', '强行从Rain后面插入', 'Tom', 'Amy', '我是新来的'] 6 >>> names[2] = '该换人了' 7 >>> names 8 ['Vison', 'Tenglan', '该换人了', 'Eric', 'Rain', '强行从Rain后面插入', 'Tom', 'Amy', '我是新来的']
列表删除:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> names 6 ['Vison', 'Tenglan', '该换人了', 'Eric', 'Rain', '强行从Rain后面插入', 'Tom', 'Amy', '我是新来的'] 7 >>> del names[2] 8 >>> names 9 ['Vison', 'Tenglan', 'Eric', 'Rain', '强行从Rain后面插入', 'Tom', 'Amy', '我是新来的'] 10 >>> del names[4] 11 >>> names 12 ['Vison', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的'] 13 >>> names.remove('Eric') #remove方法删除指定元素 14 >>> names 15 ['Vison', 'Tenglan', 'Rain', 'Tom', 'Amy', '我是新来的'] 16 >>> names.pop() #删除列表最后一个元素 17 '我是新来的' 18 >>> names 19 ['Vison', 'Tenglan', 'Rain', 'Tom', 'Amy']
列表拓展:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> names 6 ['Vison', 'Tenglan', 'Rain', 'Tom', 'Amy'] 7 >>> b = [1,2,3] 8 >>> names.extend(b) 9 >>> names 10 ['Vison', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]
列表复制:
1 >>> import copy 2 >>> origin = [1, 2, [3, 4]] 3 #origin 里边有三个元素:1, 2,[3, 4] 4 >>> cop1 = copy.copy(origin) 5 >>> cop2 = copy.deepcopy(origin) 6 >>> cop1 == cop2 7 True 8 >>> cop1 is cop2 9 False 10 #cop1 和 cop2 看上去相同,但已不再是同一个object 11 >>> origin[2][0] = "hey!" 12 >>> origin 13 [1, 2, ['hey!', 4]] 14 >>> cop1 15 [1, 2, ['hey!', 4]] 16 >>> cop2 17 [1, 2, [3, 4]] 18 #把origin内的子list [3, 4] 改掉了一个元素,观察 cop1 和 cop2
copy()为浅复制。deepcopy()为深复制。
我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在,所以改变原有被复制对象不会对已经复制出来的新对象产生影响。
而浅复制并不会产生一个独立的对象单独存在,他只会复制第一层数据,对于嵌套列表只会复制数据指针。
所以当被复制对象嵌套列表里的元素发生改变时,另一个复制对象也会随之改变。
对于简单的 object,用 shallow copy 和 deep copy 没区别。
复杂的 object, 如 list 中套着 list 的情况,shallow copy 中的 子list,并未从原 object 真的「独立」出来。
也就是说,如果你改变原 object 的子 list 中的一个元素,你的 copy 就会跟着一起变。
列表统计:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> names = ['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3] 5 >>> names 6 ['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3] 7 >>> names.count('Amy') 8 2
列表排序和翻转:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> names = ['Vison', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3] 6 >>> names.sort() #排序 7 Traceback (most recent call last): 8 File "<pyshell#2>", line 1, in <module> 9 names.sort() #排序 10 TypeError: '<' not supported between instances of 'int' and 'str' #3.0不支持不同数据类型排序 11 >>> names[-3] = '1' 12 >>> names[-2] = '2' 13 >>> names[-1] = '3' 14 >>> names 15 ['Amy', 'Amy', 'Tenglan', 'Tom', 'Vison', '1', '2', '3'] 16 >>> names.sort() 17 >>> names 18 ['1', '2', '3', 'Amy', 'Amy', 'Tenglan', 'Tom', 'Vison'] 19 20 21 >>> names.reverse() #翻转 22 >>> names 23 ['Vison', 'Tom', 'Tenglan', 'Amy', 'Amy', '3', '2', '1']
列表获取下标:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> names 5 ['Vison', 'Tom', 'Tenglan', 'Amy', 'Amy', '3', '2', '1'] 6 >>> names.index('Amy') 7 3
列表的遍历:
1 # !/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 names = ['Vison', 'Tenglan', 'Eric'] 6 for name in names: 7 print(name) 8 9 # Vison 10 # Tenglan 11 # Eric 12 13 for index, name in enumerate(names): 14 print(index, name) 15 16 # 0 Vison 17 # 1 Tenglan 18 # 2 Eric
2、Python元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表。
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> names = ('Vison', 'jack', 'eric') 5 >>> names 6 ('Vison', 'jack', 'eric')
元组只有2个方法,一个是count,一个是index。
1 ##count #统计元组字符出现的次数 2 name = ('wupeiqi', 'alex','lzl') 3 print(name.count('alex')) 4 # 1 5 ##index #查看字符串所在的索引位置 6 name = ('wupeiqi', 'alex','lzl') 7 print(name.index('lzl')) 8 # 2
元组的遍历:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 names = ('Vison','Tenglan','Eric') 6 for name in names: 7 print(name) 8 9 10 # Vison 11 # Tenglan 12 # Eric
3、Python字典
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
定义字典:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> info = { 6 'stu1': "Vison Wong1", 7 'stu2': "Vison Wong2", 8 'stu3': "Vison Wong3", 9 } 10 >>> info 11 {'stu1': 'Vison Wong1', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3'}
字典的特性:
-
- dict是无序的
- key必须是唯一的,so 天生去重
字典增加:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> info 5 {'stu1': 'Vison Wong1', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3'} 6 >>> info['stu4'] = 'Vison Wong4' 7 >>> info 8 {'stu1': 'Vison Wong1', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4'}
字典修改:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> info 6 {'stu1': 'Vison Wong1', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4'} 7 >>> info['stu1'] = 'Vison Wong1_revised' 8 >>> info 9 {'stu1': 'Vison Wong1_revised', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4'}
字典删除:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> info 5 {'stu1': 'Vison Wong1_revised', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4'} 6 >>> info.pop('stu1') 7 'Vison Wong1_revised' 8 >>> info 9 {'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4'} 10 >>> del info['stu3'] 11 >>> info 12 {'stu2': 'Vison Wong2', 'stu4': 'Vison Wong4'}
字典查找:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> info = { 6 'stu1': 'Vison Wong1', 7 'stu2': 'Vison Wong2', 8 'stu3': 'Vison Wong3', 9 'stu4': 'Vison Wong4' 10 } 11 >>> info 12 {'stu1': 'Vison Wong1', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4'} 13 >>> 'stu2' in info 14 True 15 >>> info.get('stu2') 16 'Vison Wong2' 17 >>> info['stu2'] 18 'Vison Wong2'
字典常见用法:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 >>> info = { 4 'stu1': 'Vison Wong1', 5 'stu2': 'Vison Wong2', 6 'stu3': 'Vison Wong3', 7 'stu4': 'Vison Wong4' 8 } 9 >>> info 10 {'stu1': 'Vison Wong1', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4'} 11 >>> #取值 12 >>> info.values() 13 dict_values(['Vison Wong1', 'Vison Wong2', 'Vison Wong3', 'Vison Wong4']) 14 >>> #取键 15 >>> info.keys() 16 dict_keys(['stu1', 'stu2', 'stu3', 'stu4']) 17 >>> # setdefault() 函数如果键不存在于字典中,将会添加键并将值设为默认值。 18 >>> # 如果字典中包含有给定键,则返回该键已有键值,否则返回为该键设置的值。 19 >>> info.setdefault('stu5','Vison Wong5') 20 'Vison Wong5' 21 >>> info 22 {'stu1': 'Vison Wong1', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4', 'stu5': 'Vison Wong5'} 23 >>> info.setdefault('stu1','Vison Wong1_revised') 24 'Vison Wong1' 25 >>> info 26 {'stu1': 'Vison Wong1', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4', 'stu5': 'Vison Wong5'} 27 >>> # dict.update(dict2) 把字典dict2的键/值对更新到dict里 28 >>> info1 = { 29 'stu1' : 'Vison Wong1_revised', 30 'stu6' : 'Vison Wong6' 31 } 32 >>> info.update(info1) 33 >>> info 34 {'stu1': 'Vison Wong1_revised', 'stu2': 'Vison Wong2', 'stu3': 'Vison Wong3', 'stu4': 'Vison Wong4', 'stu5': 'Vison Wong5', 'stu6': 'Vison Wong6'} 35 >>> # items() 函数以列表返回可遍历的(键, 值) 元组数组 36 >>> info.items() 37 dict_items([('stu1', 'Vison Wong1_revised'), ('stu2', 'Vison Wong2'), ('stu3', 'Vison Wong3'), ('stu4', 'Vison Wong4'), ('stu5', 'Vison Wong5'), ('stu6', 'Vison Wong6')])
字典遍历:
1 info_dic = {'stu1101': "TengLan Wu",'stu1102': "LongZe Luola",'stu1103': "XiaoZe Maliya",} 2 for stu_nu in info_dic: 3 print(stu_nu,info_dic[stu_nu]) #循环默认提取的是key 4 #stu1103 XiaoZe Maliya 5 #stu1101 TengLan Wu 6 #stu1102 LongZe Luola 7 for k,v in info_dic.items(): #先把dict生成list,数据量大的时候费时,不建议使用 8 print(k,v) 9 #stu1103 XiaoZe Maliya 10 #stu1101 TengLan Wu 11 #stu1102 LongZe Luola
补充:
1 dic = {'k1':'v1','k2':'v2','k3':'v3'} 2 3 for key,value in dic.items(): #错误的方式 4 if key == 'k2': 5 del dic[key] 6 # RuntimeError: dictionary changed size during iteration #字典在迭代的时候不能改变其长度 7 8 print(dic.keys()) # 迭代器 9 # dict_keys(['k3', 'k2', 'k1']) 10 for key in list(dic.keys()): 11 if key == 'k2': 12 del dic[key]
多级字典嵌套及操作:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 5 >>> menu = { 6 '河南': { 7 '郑州': ['金水区', '二七区', '惠济区', '中原区', '上街区'], 8 '洛阳': ['涧西区', '西工区', '老城区', '洛龙区', '吉利区'], 9 '三门峡' : ['灵宝市', '卢氏', '义马市', '陕县'] 10 }, 11 '陕西': { 12 '西安': ['碑林区', '莲湖区', '雁塔区', '未央区', '灞桥区'], 13 '渭南': ['临渭区', '华阴市', '韩城', '华县', '潼关'], 14 '宝鸡': ['涧西区', '西工区', '老城区', '洛龙区', '吉利区'] 15 } 16 } 17 18 19 >>> menu['陕西']['西安'] 20 ['碑林区', '莲湖区', '雁塔区', '未央区', '灞桥区']
4、Python集合
集合是一个无序的,不重复的数据组合,它的主要作用如下:
-
- 去重,把一个列表变成集合,就自动去重了
- 关系测试,测试两组数据之前的交集、差集、并集等关系
集合定义:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> set1 = {1,2,3,5,7,9} 5 >>> set1 6 {1, 2, 3, 5, 7, 9} 7 >>> type(set1) 8 <class 'set'> 9 >>> set2 = {2,4,6,8} 10 >>> set2 11 {8, 2, 4, 6}
集合常见用法:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> set1 5 {1, 2, 3, 5, 7, 9} 6 >>> set2 7 {8, 2, 4, 6} 8 >>> # 交集 9 >>> set1.intersection(set2) 10 {2} 11 >>> # 并集 12 >>> set1.union(set2) 13 {1, 2, 3, 4, 5, 6, 7, 8, 9} 14 >>> # 差集,在set1中,不在set2中 15 >>> set1.difference(set2) 16 {1, 3, 5, 7, 9} 17 >>> # 对称差集,在set1或set2中,但不会同时出现在二者中 18 >>> set1.symmetric_difference(set2) 19 {1, 3, 4, 5, 6, 7, 8, 9} 20 >>> # 添加一项 21 >>> set2.add(0) 22 >>> set2 23 {0, 2, 4, 6, 8} 24 >>> # 添加多项 25 >>> set1.update([11,13]) 26 >>> set1 27 {1, 2, 3, 5, 7, 9, 11, 13} 28 >>> # 删除一个元素 29 >>> set1.remove(13) 30 >>> set1 31 {1, 2, 3, 5, 7, 9, 11} 32 >>> # 测试集合长度 33 >>> len(set1) 34 7 35 >>> # 测试set1是否为set2子集 36 >>> set1.issubset(set2) 37 False 38 >>> # 测试set1是否为set2父集 39 >>> set1.issuperset(set2) 40 False
5、 列表、元组、字典的相互转换
列表的转换:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 4 >>> # 列表转元组 5 list1 = ['value1','value2','value3'] 6 >>> tuple(list1) 7 ('value1', 'value2', 'value3') 8 >>> # 列表转集合(去重) 9 >>> set(list1) 10 {'value2', 'value3', 'value1'} 11 >>> # 两个列表转字典 12 >>> list2 = ['key1','key2','key3'] 13 >>> dict(zip(list2,list1)) 14 {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} 15 >>> # 嵌套列表转字典 16 >>> list3 = [['key1','value1'],['key2','value2'],['key3','value3']] 17 >>> dict(list3) 18 {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} 19 >>> # 列表转字符串 20 >>> ''.join(list1) 21 'value1value2value3'
元组的转换:
元组的转换与列表相似,方法通用。
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 >>> # 元组转列表 4 >>> tuple1 =('value1','value2','value3') 5 >>> list(tuple1) 6 ['value1', 'value2', 'value3']
字典的转换:
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 >>> 4 >>> dict1 = { 5 'key1':'value1', 6 'key2':'value2', 7 'key3':'value3' 8 } 9 >>> # 将字典的key转换为列表 10 >>> list(dict1) 11 ['key1', 'key2', 'key3'] 12 >>> # 将字典的value转换为列表15 >>> list(dict1.values()) 16 ['value1', 'value2', 'value3'] 17 >>> # 将字典转换为字符串 18 >>> str(dict1) 19 "{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}"