python基础--5字典
#字典
#dict
#基本结构
info={
"k1":'v1' #键值对
,"k2":"v2"
}
#字典的value可以是任何值
# info={"k1":18,
# 'k2':True,
# 'k3':[11,22,55,{"kk1":'vv1',
# 'kk2':'vv2',
# 'kk3':(11,33,55)}],
# "k4":(1,23)
# }
####,列表,字典不能作为字典的key
# info={1:"asdf",
# "k1":"asdf",
# (11,22):123,
# # True:123
# #{"ll":"hello"}:123
# #[11,22]:22
# }
# print(info)
#字典无序
#字典支持del删除
# info={"k1":18,
# 'k2':True,
# 3:"sss",
# 'k3':[11,22,55,{"kk1":'vv1',
# 'kk2':'vv2',
# 'kk3':(11,33,55)}],
# "k4":(1,23)
# }
#通过索引找到指定元素
# print(info['k1'])
# print(info[3])
#print(info["k3"][3]["kk3"][0])
#字典支持del删除
# del info["k1"]
# print(info)
#for 循环
#dict
info={"k1":18,
'k2':True,#布尔值(0,1) key值重复则只显示一个
3:"sss",
'k3':[11,22,55,{"kk1":'vv1',
'kk2':'vv2',
'kk3':(11,33,55)}],
"k4":(1,23)
}
# for item in info.values():
# print(item)
# for item in info.keys():
# print(item)
# for k,v in info.items():
# print(k,v)
# num="k1" in info
# print(num)
############
#字典
#dict
#根据序列,创建字典,指定统一的值!!!
# v=dict.fromkeys([11,22,33],"han")
# print(v)
#根据Key获取值,key不存在是,可以指定默认值(None)
dic={"item":1,"k1":1222}
# n=dic.get("item11")
# print(n)
#删除并获取值
# v=dic.pop("k1")
# print(dic,v)
# v=dic.popitem()
# print(dic,v)
#设置值,如果已经存在,不设置,获取当前key对应的值
#不存在,设置,获取当前key对应的值
# v=dic.setdefault("k12","han")
# print(dic,v)
#更新
#dic.update({'k1':1111,"k3":12223})
dic.update(k1=123,k3=2553)#**kwargs
print(dic)
#keys() ,values() ,items() ,get() ,update
#字典的in方法
# v=1 in dic.values()
# print(v)
#布尔值
#0,1
#0 ,'',None,{},(),[] ==> False
如果我失败了,至少我尝试过,不会因为痛失机会而后悔