dictionary={"qq":"1050361","手机":"182865","邮箱":"cloud.com"}
print(dictionary)
a=[2,3,4]
b=["ni","wo","ta"]
c=dict(zip(a,b)) #用zip把两个列表创建为字典
print(c)
d=dict(wo ="niba",ta ="nima")
print(d) #用键值对创建字典
e=dict.fromkeys(b) #fromkeys函数创建值为空对字典
print(e)
g=(2,3,4)
f={g:b} #元组和列表创建字典
print(f)
print(dictionary["qq"])
print(c[2])
print("qq是",dictionary["qq"]if "qq"in dictionary else "没有qq") #if
print("QQ是",dictionary.get("QQ","没有QQ")) #get()方法
for item in dictionary.items(): #用items()函数遍历
print(item)
dictionary["地址"]="hainan" #添加字典元素
print(dictionary)
del dictionary["qq"] #删除元素
print(dictionary)
h={i:j+"🐎" for i,j in zip(a,b) } #字典推导式
print(h)
python字典
最新推荐文章于 2021-11-28 09:12:52 发布