# 字典推导式

'''
语法一:
key:字典中的key
value:字典中的value
dict.items():序列
condition:条件表达式
key_exp:在for循环中,如果条件表达式condition成立(即条件表达式成立),返回对应的key,value并作key_exp,value_exp处理
value_exp:在for循环中,如果条件表达式condition成立(即条件表达式成立),返回对应的key,value并作key_exp,value_exp处理

{key_exp: value_exp for key, value in dict.items() if condition}

语法二:
key:字典中的key
value:字典中的value
dict.items():序列
condition:条件表达式
key_exp:在for循环中,如果条件表达式condition成立(即条件表达式成立),返回对应的key,value并作key_exp,value_exp处理
value_exp1:在for循环中,如果条件表达式condition成立(即条件表达式成立),返回对应的key,value并作key_exp,value_exp1处理
value_exp2:在for循环中,如果条件表达式condition不成立(即条件表达式不成立),返回对应的key,value并作key_exp,value_exp2处理

{key_exp: value_exp1 if condition else value_exp2 for key, value in dict.items()}
'''
# 案例一:获取字典中key值是小写字母的键值对
dict1 = {"a":10,"B":20,"C":True,"D":"hello world","e":"python"}
dict2 = {key:value for key,value in dict1.items() if key.islower()}
print(dict2)

# 案例二:将字典中的所有key设置为小写
dict3 = {"a":10,"B":20,"C":True,"D":"hello world","e":"python"}
dict4 = {key.lower():value for key,value in dict3.items()}
print(dict4)

# 案例三:将字典中所有key是小写字母的value统一赋值为'error'
dict5 = {"a":10,"B":20,"C":True,"D":"hello world","e":"python"}
dict6 = {key:value if key.isupper() else "error" for key,value in dict5.items()}
print(dict6)