#为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(1,[1,3],‘sss‘,(1,2)) #t=tuple((1,[1,3],‘sss‘,(1,2))) # print(type(t)) # #元组可以作为字典的key # d={(1,2,3):‘egon‘} # print(d,type(d),d[(1,2,3)]) # 切片 # goods=(‘iphone‘,‘lenovo‘,‘sanxing‘,‘suoyi‘) # print(goods[1:3]) # 长度 #in: #字符串:子字符串 #列表:元素 #元组:元素 #字典:key # goods=(‘iphone‘,‘lenovo‘,‘sanxing‘,‘suoyi‘) # print(‘iphone‘ in goods)看的是里面的元素在不在里面 # d={‘a‘:1,‘b‘:2,‘c‘:3} # print(‘b‘ in d) 看的是key在不在d里面 #掌握 # goods=(‘iphone‘,‘lenovo‘,‘sanxing‘,‘suoyi‘) # print(goods.index(‘iphone‘)) # print(goods.count(‘iphone‘)) #补充:元组本身是不可变的,但是内部的元素可以是可变类型 t=(1,[‘a‘,‘b‘],‘sss‘,(1,2)) #t=tuple((1,[1,3],‘sss‘,(1,2))) # t[1][0]=‘A‘ # print(t) # t[1]=‘aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa‘
字典
字典常用方法
字典的表示方法: info_dic={‘name‘:‘haiyna‘,‘age‘:18,‘sex‘:‘female‘} # 常用操作: # 1.存/取 info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} print(info_dic[‘name11111111‘])#找不到则报错了 print(info_dic.get(‘name‘,None)) print(info_dic.get(‘name222222‘,None))#get方法找不到不报错,可以自己设定默认值 #pop:key存在则弹出值,不存在则返回默认值,如果没有默认值则报错 # print(info_dic.pop(‘nam123123123123123123e‘,None)) # print(info_dic) # print(info_dic.popitem()) # print(info_dic) # info_dic[‘level‘]=10 # print(info_dic) # # 删除 info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} # info_dic.pop() # info_dic.popitem() # del info_dic[‘name‘] # # 键s,值s,键值对 info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} # print(info_dic.keys()) # print(info_dic.values()) # print(info_dic.items()) # for k in info_dic: # # print(k,info_dic[k]) # print(k) # print(‘========>‘) # for k in info_dic.keys(): # print(k) # for val in info_dic.values(): # print(val) # for k,v in info_dic.items(): #k,v=(‘name‘, ‘egon‘) # print(k,v) # 长度 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} # print(len(info_dic)) # # 循环 # # 包含in # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} # print(‘name‘ in info_dic) # print(‘name‘ in info_dic.keys()) # print(‘egon‘ in info_dic.values()) # print((‘name‘,‘egon‘) in info_dic.items()) #掌握 info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} # info_dic.update({‘a‘:1,‘name‘:‘Egon‘}) # print(info_dic) # info_dic[‘hobbies‘]=[] # info_dic[‘hobbies‘].append(‘study‘) # info_dic[‘hobbies‘].append(‘read‘) # print(info_dic) #setdefault:key不存在则设置默认值,并且放回值默认值 #key存在则不设置默认,并且返回已经有的值 # info_dic.setdefault(‘hobbies‘,[1,2]) # print(info_dic) # info_dic.setdefault(‘hobbies‘,[1,2,3,4,5]) # print(info_dic) # info_dic={‘name‘:‘haiyan‘,‘age‘:18,‘sex‘:‘male‘} # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘]} # info_dic.setdefault(‘hobbies‘,[]).append(‘study‘) # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘,‘read‘]} # info_dic.setdefault(‘hobbies‘,[]).append(‘read‘) # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘,‘read‘,‘sleep‘]} # info_dic.setdefault(‘hobbies‘,[]).append(‘sleep‘) # l=info_dic.setdefault(‘hobbies‘,[]) # print(l,id(l)) # print(id(info_dic[‘hobbies‘])) # print(info_dic) #了解 # d=info_dic.copy() # print(d) # info_dic.clear() # print(info_dic) # d=info_dic.fromkeys((‘name‘,‘age‘,‘sex‘),None) # print(d) # d1=dict.fromkeys((‘name‘,‘age‘,‘sex‘),None) # d2=dict.fromkeys((‘name‘,‘age‘,‘sex‘),(‘egon‘,18,‘male‘)) # print(d1) # print(d2) # info=dict(name=‘haiyan‘,age=18,sex=‘male‘) # print(info) # # info=di
集合set
一、集合的作用
知识点回顾:可变类型是不可hash类型,不可变类型是可hash类型
作用:去重,关系运算
定义:可以包含多个元素,用逗号分割,集合的元素遵循三个原则:
1.每个元素必须是不可变类型(可hash,可作为字典的key)
2.没有重复的元素
4.无序
注意集合的目的是将不同的值存放在一起,不同的集合间用来做关系运算,无需纠结集合中的单个值
二、常用方法
in 和 not in
|并集(print(pythons.union(linuxs)))
&交集(print(pythons.intersection(linuxs)))
-差集(print(pythons.difference(linuxs)))
^对称差集(print(pythons.symmetric_difference(linuxs)))
==
>,>= ,<,<= 父集(issuperset),子集(issuberset)
1
2
3
4
5
6
7
8
9
10
11
12
|
>,> = ,<,< = set1 = { 1 , 2 , 3 , 4 , 5 } set2 = { 1 , 2 , 3 , 4 } print (set1 > = set2) print (set1.issuperset(set2)) set1 = { 1 , 2 , 3 , 4 , 5 } set2 = { 1 , 2 , 3 } print (set1< = set2) print (set1.issubset(set2)) |
三、练习
一.关系运算
有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
pythons={‘alex‘,‘egon‘,‘yuanhao‘,‘wupeiqi‘,‘gangdan‘,‘biubiu‘}
linuxs={‘wupeiqi‘,‘oldboy‘,‘gangdan‘}
1. 求出即报名python又报名linux课程的学员名字集合
2. 求出所有报名的学生名字集合
3. 求出只报名python课程的学员名字
4. 求出没有同时这两门课程的学员名字集合
1
2
3
|
pythons = { ‘haiyan‘ , ‘fank‘ , ‘yaling‘ , ‘lalal‘ , ‘haidong‘ , ‘biubiu‘ } linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } # 1. 求出即报名python又报名linux课程的学员名字集合 |
1
2
|
pythons = { ‘haiyan‘ , ‘fank‘ , ‘yaling‘ , ‘lalal‘ , ‘haidong‘ , ‘biubiu‘ } linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } |
1
2
|
print (pythons & linuxs) # 2. 求出所有报名的学生名字集合 |
1
2
|
pythons = { ‘haiyan‘ , ‘fank‘ , ‘yaling‘ , ‘lalal‘ , ‘haidong‘ , ‘biubiu‘ } linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } |
1
2
|
print (pythons | linuxs) # 3. 求出只报名python课程的学员名字 |
1
2
|
pythons = { ‘haiyan‘ , ‘fank‘ , ‘yaling‘ , ‘lalal‘ , ‘haidong‘ , ‘biubiu‘ } linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } |
1
2
|
print (pythons - linuxs) # 4. 求出没有同时这两门课程的学员名字集合 |
1
2
|
pythons = { ‘haiyan‘ , ‘fank‘ , ‘yaling‘ , ‘lalal‘ , ‘haidong‘ , ‘biubiu‘ } linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } |
1
|
print (pythons ^ linuxs) |
四、方法
1
|
# ========掌握部分======= |
1
|
linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } |
1
2
3
4
5
|
linuxs.add( ‘xiaoxiao‘ ) #说明set类型的集合是可变类型 linuxs.add([ 1 , 2 , 3 ]) #报错,只能添加不可变类型 print (linuxs) # 2. |
1
|
linuxs = { ‘six‘ , ‘wu‘ , ‘dabao‘ } |