Python--基本的对象类型(字典_可变的数据类型)
1.1.7字典
以下方法均在python解释器中进行了测试,读者复制代码时,记得去掉注释符。
#!/usr/bin/env python # -*- coding: utf-8 -*- # ******************通过dict类创建的对象****************** # dict 类 字典 # 1:字典的格式 # di = {"name":"ae",123:"23",True:12,(12,34):"qq"} # print(di) # 2:字典中的元素用花括号括起来,其中的元素由键值对组成,键keys,值valuse # 字典的"键"只能是不可变的对象,比如数字,字符串,元组,布尔值,而且键不能重复,如果重复则只会显示第一个键值对; # 字典的"值"可以是任意元素,值可以重复 # 注意:字典中的元素是无序的,所以不能进行切片操作 # # 3:创建字典 # di1 = {"name":"ae",123:"23",True:12,(12,34):"qq"} #1:直接创建 # li1 = [("name","qingyu"),["age","20"]] #元组和列表都可以,但是必须大于2项 # di2 = dict(tu) #2:利用dict()函数构建字典 # di3 = dict(name = "qingyu",age = 12,a = [11,22,33,44]) #2:键是字符串,但是不用带引号,值如果是字符串需要带引号 # print(di1,di2,di3,sep = "\n") # x = ["name","age"] # y = ["qingyu",20] # di4 = dict(zip(x,y)) #3:使用zip函数将名/值列表链接在一起,来创建字典 # print(di4) # di5 = dict.fromkeys(["11","22","33"],"ae") #4:根据序列来创建字典,并指定统一的值; # di6 = dict.fromkeys("qwer",11) #注意:formkeys中的是可迭代的键 # di7 = dict.fromkeys(("11","22","33",),"ae") # print(di5,di6,di7,sep = "\n") # 4:通过索引找到元素,索引的是键;字典支持del删除键值对 # di = {11:"22","qwer":"skill",22:[112,113,"qw",{1:"ae",2:"qingyu",3:45}]} # print(di[22][3][2]) # print(di) # del di[22][3][2] # print(di) # 5:字典是可迭代对象,可以通过for循环遍历 # di = {"a1":"qwer","k2":111,3:[11,22,33]} # for i1 in di: #默认是打印的键 # print(i1) # for i2 in di.keys(): #打印键 # print(i2) # print(di[i2]) #索引打印值 # for i3 in di.values(): #打印值 # print(i3) # for i4 in di.items(): #打印键值对,会将键值对放在元组里 # print(i4) # for x,y in di.items(): #打印键值对 # print(x,y) # 其他:in和not in 可以判断键是否在字典中;len()求字典的长度等 # ******************dict类中提供的方法****************** # 1:clear方法:清空元素 # di = {"a1":"qwer","k2":111,3:[11,22,33]} # print(di.clear()) #返回值是None # 2:copy方法:浅拷贝,类似于列表的浅拷贝,只能拷贝一级元素 # di1 = {"a1":"qwer","k2":111,3:[11,22,33]} # di2 = di1.copy() # 浅拷贝如果有嵌套,嵌套内容的更改会同时影响到拷贝方和被拷贝方,因为指向同一个地址 # print(di2) # 3:fromkeys方法:通过序列创建字典,并统一指定值; # di1 = dict.fromkeys(["11","22","33"],"ae") # di2 = dict.fromkeys("qwer",11) # di3 = dict.fromkeys(("11","22","33",),"ae") # print(di1,di2,di3,sep = "\n") # di4 = dict.fromkeys(("q",[1,2],),11) #这种情况会创建失败,因为[1,2]是可哈希的 # print(di4) #注意:formkeys中的是可迭代的键 # 4:get方法:根据键获取值,键不存在时,可以指定默认值,默认值为None # di = {"a1":"qwer","k2":111,3:[11,22,33]} # a1 = di.get('k1',111111) # a2 = di.get("k2") # a3 = di.get("k3") # print(a1,a2,a3) # 5:keys方法,valuse方法,items方法 # di = {"a1":"qwer","k2":111,3:[11,22,33]} # for i1 in di: #默认是打印的键 # print(i1) # for i2 in di.keys(): #打印键 # print(i2) # print(di[i2]) #索引打印值 # for i3 in di.values(): #打印值 # print(i3) # for i4 in di.items(): #打印键值对,会将键值对放在元组里 # print(i4) # for x,y in di.items(): #打印键值对 # print(x,y) # 6:pop方法:按照键来删除键值对,并获取键值对中的值 # di = {"a1":"qwer","k2":111,3:[11,22,33]} # a1 = di.pop("a1") # print(di,a1) # 7:popitem方法:从末尾删除键值对,并返回键和值 # di = {"a1":"qwer","k2":111,3:[11,22,33]} # a = di.popitem() # print(a) # di = {"a1":"qwer","k2":111,3:[11,22,33]} # x,y = di.popitem() # print(x,y) # # 8:setdefault方法:设置值,如果,将会添加键并将值设为默认值; # di = {"a1":"qwer","k2":111,3:[11,22,33]} # a1 = di.setdefault('k1',111111) #键不存在于字典中,添加键并将值设为111111; # print(di) # a2 = di.setdefault("k2") #如果键存在的话,会获取值 # print(di) # a3 = di.setdefault("k3") #键不存在于字典中,设置默认值None # print(di) # print(a1,a2,a3) # 9:update方法:把字典2中的键值对更新到字典1中去 # di1 = {"k1":"q","k2":"w",3:"e",4:"r"} # print(di1) # di2 = {"k1":"a","k2":"s","k3":3333} # di1.update(di2) #若键不存在,则在末尾追加键值对 # print(di1)
为了将来做自己想做的事情努力.......