# 按照属性存放多个值,在{}内用逗号分隔开多个key:value# 其中value可以是任意类型# 而key必须是不可变的类型,通常为字符串类型,并且key不能重复
info ={'name':'wuchangwen','age':18,'gender':'male'}print(info)
4.2 字典常用的内置方法
# 1.类型转换
info =dict([['name','wuchangwen'],('age',18)])print(info,type(info))# 2.按key存取值:可存可取
dic ={'name':'wuchangwen','age':18}print(dic['name'])print(dic['nameasdfadf'])
res=dic.get('name')print(res)# key不存在则加元素,存在则改值
dic['gender']='male'print(dic)# 3.长度len
dic ={'name':'wuchangwen','age':18}print(len(dic))# 4.成员运算in和not in=>依据key
dic ={'name':'wuchangwen','age':18}print('name'in dic)print('wuchangwen'in dic)4、删除
dic ={'name':'wuchangwen','age':18}# 方式1:万能删除,没有返回值del dic['name']print(dic)# 方式2:pop指定key删除,返回删除的value
res = dic.pop('name')print(res)# 方式3:popitem
res=dic.popitem()print(res)# 6.键keys(),值values(),键值对items()
dic ={'name':'wuchangwen','age':18}
dic.keys()
C:\Users\wu先生>python2
Python 2.7.18(v2.7.18:8d21aa21f2, Apr 202020,13:25:05)[MSC v.150064 bit (AMD64)] on win32
Type "help","copyright","credits"or"license"for more information.>>>>>> dic ={'name':'wuchangwen','age':18}>>> dic
{'age':18,'name':'wuchangwen'}>>>>>> dic.keys()['age','name']>>> dic.values()[18,'wuchangwen']>>> dic.items()[('age',18),('name','wuchangwen')]>>># 7.循环
dic ={'name':'wuchangwen','age':18}for k in dic.keys():print(k)print(list(dic.keys()))for v in dic.values():print(v)for k,v in dic.items():print(k,v)# update
dic={'k1':111,'k2':2222}
dic.update({"k2":4444,'k3':5555})print(dic)
dic={'age':18}# if 'name' not in dic:# dic['name']="wuchangwen"
res = dic.setdefault('name',"wuchangwen")# print(dic)print(res)
五、元组类型(tuple)
5.1 重要概念(不可变,有序)
1.作用:按照索引/位置存放多个值,只用于读,不用于改
2.定义:
t =(1,1.3,'aaa')#t = tuple((1,1.3,'aaa'))print(t,type(t))
单独一个括号,代表包含的意思
x =(10)#单独一个括号,代表包含的意思print(x,type(x))
如果元组中只有一个元素,必须加逗号
x =(10,)#如果元组中只有一个元素,必须加逗号print(x,type(x))
t =(1,1.3,'aaa')#t = (0->值1的内存地址,1->值1.3的内存地址,2->值'aaa'的内存地址,)#只要元组内元素的内存地址没变,元组就是不可变
元组不能改,指的是不能改里面的内存地址
t =(1,[11,22])print(t,id(t[0]),id(t[1]))
t[1][0]=33# 元组内的列表的元素还是可以改的print(t,id(t[0]),id(t[1]))(1,[11,22])203888427259297864(1,[33,22])203888427259297864
5.2 元组常用的内置方法
# 1.类型装换print(tuple('hello'))print(tuple([1,2,3]))print(tuple({'name':'wuchangwen','age':18}))# 2.按照索引取值(正向取值 + 反向取值)只能取
t =(111,'egon','hello')# 正向取值print(t[0])# 反向取值print(t[-1])# 3.切片(顾头不顾尾,步长)
t =('aa','bbb','cc','dd','eee')print(t[0:3])print(t[::-1])# ('aa', 'bbb', 'cc')# ('eee', 'dd', 'cc', 'bbb', 'aa')# 4.长度
t =('aa','bbb','cc','dd','eee')print(len(t))# 5# 5.成员运算 in 与 not in 判断一个子字符串是否存在于一个大字符串中
t =('aa','bbb','cc','dd','eee')# Trueprint('aa'in t)# 6.循环
t =('aa','bbb','cc','dd','eee')for x in t:print(x)# aa# bbb# cc# dd# eee# 7.index和count补充:# index:找出该字符串在元组中的索引位置,找不到就会报错
t =(2,3,111,111,111,111)print(t.index(111))print(t.index(1111111111))# ValueError: tuple.index(x): x not in tuple# count:统计该字符串在元组中出现的次数
t =(2,3,111,111,111,111)print(t.count(111))# 4
六、集合类型(set)
6.1 重要概念(不可变)
作用:关系运算、去重
定义:在 {} 内通逗号隔开多个元素,多个元素满足以下条件:
1.集合内的元素必须为不可变类型
# s = {1,[1,2]} # 错误,集合内的元素必须为不可变类型
TypeError: unhashable type:'list'2.集合内的元素无序
s ={1,'a','z','b',4,7}# 集合内元素无序{1,4,7,'z','b','a'}3.集合内的元素没有重复
s ={1,1,1,1,'a','z','b',4,7}# 集合内的元素不能重复{1,'a',4,7,'z','b'}
了解
s ={}# 默认是空字典print(type(s))
s ={1,2}# s = set({1,2})
定义空集合:
s =set()print(s,type(s))
类型转换
set({1,2,3})
res =set('hellollll')# 可以去重print(res){'e','o','h','l'}print(set([1,1,1,1,1,1,1])){1}print(set([1,1,1,1,1,1,1,[11,222]]))
TypeError: unhashable type:'list'print(set({'k1':1,'k2':2})){'k1','k2'}
6.2 集合常用的内置方法
# 1.关系运算符# ① 取出交集:两者共同的好友
friends1 ={"wuchnagwen","wuchangchang","jack","tom"}# 用户1的好友们
friends2 ={"wuchangwen","tony","tom","mack"}# 用户2的好友们
res = friends1 & friends2
res = friends1.intersection(friends2)print(res)# ② 取出并集/交集:两者所有的好友
friends1 ={"wuchnagwen","wuchangchang","jack","tom"}# 用户1的好友们
friends2 ={"wuchangwen","tony","tom","mack"}# 用户2的好友们
res = friends1 | friends2
res = friends1.union(friends2)print(res)# ③ 取出差集:取出其中一个集合独有的好友# 取出 friends1 独有的好友:
friends1 ={"wuchnagwen","wuchangchang","jack","tom"}# 用户1的好友们
friends2 ={"wuchangwen","tony","tom","mack"}# 用户2的好友们
res = friends1 - friends2
res = friends1.difference(friends2)print(res)# 取出 friends2 独有的好友:
friends1 ={"wuchnagwen","wuchangchang","jack","tom"}# 用户1的好友们
friends2 ={"wuchangwen","tony","tom","mack"}# 用户2的好友们
res = friends2 - friends1
res = friends1.symmetric_difference(friends2)print(res)# ④ 对称差集:求2个用户独有的好友们(即:去掉共同的好友)
friends1 ={"wuchnagwen","wuchangchang","jack","tom"}# 用户1的好友们
friends2 ={"wuchangwen","tony","tom","mack"}# 用户2的好友们
res = friends2 ^ friends1
print(res)# ⑤ 父子集:包含的关系
s1 ={1,2,3}
s2 ={1,2,4}# 不存在包含关系,下面比较均为Falseprint(s1.issupersets2())print(s1 > s2)print(s1 < s2)
s1 ={1,2,3}
s2 ={1,2}print(s1.issubset(s2))print(s1 > s2)# 当s1大于或等于s2时,才能说s1是s2的爹
s1={1,2,3}
s2={1,2,3}print(s1 == s2)# s1与s2互为父子# 2.去重# ① 只能针对不可变类型去重print(set([1,1,2,2,2]))# ② 无法保证原来的顺序
l =[1,'a','b',1,1,2,3]
l =list(set(l))print(l)[1,2,3,'a','b']# 3.长度>>> s={'a','b','c'}>>>len(s)# 4.成员运算>>>'c'in s
True# 5.循环>>>for item in s:...print(item)...
c
a
b
5.其他内置方法
.discard() —— 需要掌握
s ={1,2,3}
s.discard(4)# 删除元素不存在,do nothingprint(s).remove()
s ={1,2,3}
s.remove(4)# 删除元素不存在,则会报错print(s).update() —— 需要掌握
s ={1,2,3}
s.update({1,3,5}).pop() —— 需要掌握
s ={1,2,3}
res = s.pop()print(res).add() —— 需要掌握
s ={1,2,3}
s.add(4)print(res)