标签:bbb type userinfo inf pre not 取值 aaa ict
#一:基本使用dict # 1 用途:记录多个值,每一个值都对应的key用来描述value的作用 # # 2 定义方式:在{}内用逗号分隔开多 个key:value,其中value可以是任意类型,而key必须是不可变的类型,通常情况下应该str类型 # dic={0:‘aaa‘,1:‘bbb‘,2:‘cccc‘} #dic=dict({0:‘aaa‘,1:‘bbb‘,2:‘cccc‘}) # print(dic,type(dic)) # print(dic[0]) # dic={[1,2]:‘aaa‘} # dic={(1,2):‘aaa‘} # print(dic[(1,2)]) # 用法一: # dic=dict(x=1,y=2,z=3) # print(dic) # 用法二: # userinfo=[ # [‘name‘,‘egon‘], # [‘age‘,18], # [‘sex‘,‘male‘] # ] # d={} # for k,v in userinfo: #k,v=[‘name‘, ‘egon‘] # # print(k,v) # d[k]=v # print(d) # d=dict(userinfo) # print(d) # 3 常用操作+内置的方法 #优先掌握的操作: #1、按key存取值:可存可取 # dic={‘name‘:‘egon‘} # print(dic[‘name‘]) # dic[‘name‘]=‘EGON‘ # print(dic) # dic[‘age‘]=18#字典索引不存在时不会像列表那样报错,字典会将其填进去 # print(dic) # l=[‘a‘,‘b‘] # l[0]=‘A‘ # l[2]=‘c‘ #2、长度len # dic={‘name‘:‘egon‘,‘age‘:18,‘name‘:‘EGON‘,‘name‘:‘XXXX‘}#key不能重复 # print(dic) # print(len(dic)) #3、成员运算in和not in:字典的成员运算判断的是key dic={‘name‘:‘egon‘,‘age‘:18,} print(18 in dic) print(‘age‘ in dic) #4、删除 # dic={‘name‘:‘egon‘,‘age‘:18,} # 通用 # del dic[‘name‘] # print(dic) # del dic[‘xxx‘] ##key不存在则报错 # res=dic.pop(‘age‘) #删除key对应的value,并返回value # print(dic) # print(res) # # dic.pop(‘xxx‘) #key不存在则报错 # res=dic.popitem()#随机删 # print(dic) # print(res) 返回一个元组,第一个值是KEY,第二个是对用的value #5、键keys(),值values(),键值对items() # dic={‘name‘:‘egon‘,‘age‘:18,} # print(dic.keys()) # l=[] # for k in dic.keys(): # l.append(k) # print(l) # print(list(dic.keys())) # print(dic.values()) # print(list(dic.values())) # print(dic.items()) # print(list(dic.items())) #6、循环 dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} # for k in dic.keys(): # print(k,dic[k]) # for k in dic: # print(k,dic[k]) # for v in dic.values(): # print(v) # for k,v in dic.items(): # print(k,v) #8 dic.get() dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} # dic[‘xxx‘]
标签:bbb type userinfo inf pre not 取值 aaa ict
原文地址:https://www.cnblogs.com/Hale-wang/p/10216732.html