Python系列-字典

目录

定义字典

字典的使用

创建字典

使用dict()函数和zip()函数可以通过已有数据快速创建字典

通过给定的键值对创建字典

删除字典

使用del删除字典

使用clear()删除字典里的全部元素

使用pop()删除并返回指定的"键"的元素

使用popitem()方法删除并返回字典中的一对键值对

访问字典

采用if语句对键不存在的情况进行处理

get()方法返回键对应的值

修改字典元素

添加字典元素

字典推导式

交换键值对

字典的其他操作

遍历字典

使用items()方法获取字典的键值对的元组列表

遍历值或键,values(),keys()

使用copy()方法返回一个具有相同键值对的新字典

sorted()对字典中的键进行排序

print("-------定义字典--------")
#创建空字典
message=dict()
print(message,type(message))
print("-----------")
clothes=["shirt","shorts","skirt","coat"]
collcations=dict.fromkeys(clothes)
print(collcations,type(collcations))

print("-----字典的使用-------")
#创建字典
#使用dict()函数和zip()函数可以通过已有数据快速创建字典
clothes=["shirt","shorts","skirt","coat"]
color=["white","black","yellow","brown"]
collcation=dict(zip(clothes,color))
print(collcation,type(collcation))
#通过给定的键值对创建字典
collocation=dict(shirt="white",shorts="black",skirt="yellow",coat="brown")
print(collocation,type(collocation))

#删除字典
#使用del删除字典
message={"school":"学校","grade":"年级","class":"班级"}
del message 
#使用clear()删除字典里的全部元素
message={"school":"学校","grade":"年级","class":"班级"}
message.clear() 
print(message) 
#使用pop()删除并返回指定的"键"的元素
message={"school":"学校","grade":"年级","class":"班级"}
print(message)
pop_obj1=message.pop("school") 
print(pop_obj1)
print(message)
print("-----------")
#使用popitem()方法删除并返回字典中的一对键值对
pop_obj2=message.popitem()
print(pop_obj2)
print(message)

#访问字典
taste={"apple":"sweet","lemon":"tart","salt":"salty","pepper":"spicy"}
print(taste)
print(taste["lemon"])
#当字典里不存在访问的键值对,采用if语句对键不存在的情况进行处理,即给一个默认值
taste={"apple":"sweet","lemon":"tart","salt":"salty","pepper":"spicy"}
print(taste["pear"] if 'pear' in taste else '该字典里没有该元素')
#get()方法返回键对应的值,不存在的键返回默认值,若无默认值返回None
taste={"apple":"sweet","lemon":"tart","salt":"salty","pepper":"spicy"}
print("apple的味道是:",taste.get("apple"))
print("pear的味道是:",taste.get("pear"))
print("pear的味道:",taste.get("pear","未知"))

#修改字典元素
poet={"陶渊明":"东晋","李白":"唐朝","范仲淹":"北宋","文天祥":"南宋"}
print(poet)
poet["李白"]="诗仙"
print(poet)

#添加字典元素
poet={"陶渊明":"东晋","李白":"唐朝","范仲淹":"北宋","文天祥":"南宋"}
print(poet)
poet["王国维"]="清末"
print(poet)

#删除字典元素
poet={"陶渊明":"东晋","李白":"唐朝","范仲淹":"北宋","文天祥":"南宋"}
print(poet)
del poet["李白"]
print(poet) 

print("--------字典推导式-------") 
x1={a:a**2 for a in range(10)}
print(x1)
print("-----------")
x2={v:k for k,v in x1.items()}  #交换键值对
print(x2)

print("----字典的其他操作-----")
#遍历字典
#使用items()方法获取字典的键值对的元组列表
taste={"apple":"sweet","lemon":"tart","salt":"salty","pepper":"spicy"}
for item in taste.items():
    print(item)
for key,value in taste.items():
    print(key,"的味道是",value)
#遍历值或键,values(),keys()
taste={"apple":"sweet","lemon":"tart","salt":"salty","pepper":"spicy"}
print(taste.values())
for value in taste.values():
    print(value)
print(taste.keys())
for key in taste.keys():
    print(key)
#使用copy()方法返回一个具有相同键值对的新字典
taste1={"apple":"sweet","lemon":"tart","salt":"salty","pepper":"spicy"}
taste2=taste1
taste3=taste1.copy()
print("taste1:",taste1)
print("taste2",taste2)
print("taste3",taste3)
print("------------")
print("change taste2")
taste2["tomato"]="bland"
print("taste1:",taste1)
print("taste2",taste2)
print("taste3",taste3)
print("------------------")
print("change taste3")
taste3["coffee"]="fragrant"
print("taste1:",taste1)
print("taste2",taste2)
print("taste3",taste3)
#sorted()对字典中的键进行排序
taste={"apple":"sweet","lemon":"tart","salt":"salty","pepper":"spicy"}
for item in sorted(taste.keys()):
    print(item)

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值