# =============================================================================
# create a mapping of state to abbreviation#创建一个映射
# =============================================================================
states = {#州
"陕西": "陕",
"湖北": "鄂",
"广州": "粤",
}
# =============================================================================
# create a basic set of states and some cities in them#找到一个简称州 和它里面的一个城市
# =============================================================================
cities = {#城市
"陕": "西安",
"鄂": "十堰",
"粤": "东莞",
}
# =============================================================================
# #添加更多的 城市给cities字典添加更多的元素
# =============================================================================
cities["湘"] = "湖南"#湘是键,湖南是值
cities["赣"] = "江西"
cities["黑"] = "黑龙江"
# =============================================================================
#字典通过键打印出值,打印出一些城市
# =============================================================================
print("-" * 10)
print("陕西 有: ", cities["陕"])#字典键是“陕”打印出值西安
print("湖北 有: ", cities["鄂"])#字典键是“鄂”打印出值十堰
# =============================================================================
# print some states#打印一些状态
# =============================================================================
print("-" * 10)
print("陕西's 简称: ", states["陕西"])#陕西的简写“陕”字典键是陕西,值是陕
print("湖北's 简称: ", states["湖北"])#字典键是“湖北”输出值是鄂
# =============================================================================
# do it by using the state then cities dict#是通过使用州和城市的词典
# =============================================================================
print("-" * 10)
print("湖北省有:", cities[states["湖北"]])#连环取值,字典省的键是湖北,取值鄂,字典cities键是鄂,取值十堰
print("陕西省有: ", cities[states["陕西"]])#连环取值,字典省的键是陕西,取值陕,字典cities键是陕,取值西安
# =============================================================================
# print every state abbreviation#打印每个州的缩写
# =============================================================================
print("-" * 10)
for state, abbrev in list(states.items()): # 定义变量state州和abbrev缩写,items()函数是个新东西
print(f"{state}省简称 {abbrev}")#for循环遍历字典state,打印出每个州的缩写
# =============================================================================
# print every city in state#打印每个州的城市
# =============================================================================
print("-" * 10)
for abbrev, city in list(cities.items()):# 定义变量城市和city和abbrev缩写,items()函数是个新东西
print(f"{abbrev} 有城市 {city}")#for循环遍历字典city,打印出每个州的缩写
# =============================================================================
# now do both at the same time
# =============================================================================
print("-" * 10)
# for state, abbrev in list(states.items()):
# for state, abbrev in states.items(): #这个代码也可以运行成功
for (state, abbrev) in states.items(): #打印出州和缩写 这个代码也可以运行成功
print(f"{state} 省的简称是 {abbrev}")#打印出州和缩写
print(f"有城市 {cities[abbrev]}")#字典城市cities键缩写取值每个州包含的城市
# =============================================================================
# states.items()
# Out[6]: dict_items([('Oregon', 'OR'), ('Florida', 'FL'), ('Carlifornia', 'CA'), ('New York', 'NY'), ('Michigan', 'MI')])
# 看到items()函数给出键值对组成的元祖不可动,然后再组成一个list
# 这里list函数
# 以上三行代码都可以,加括号表示tuple类型,list()函数转换成列表
#
# =============================================================================
print("-" * 10)
# =============================================================================
# safely get a abbreviation by state that might not be there#写出每个城市的缩写
# =============================================================================
state = states.get("Texas")#德克萨斯州
if not state:
print("Sorry, no Texas.")
# =============================================================================
# get a city with a default value 不存在给出默认值
# Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值
# =============================================================================
city = cities.get("陕", "Does Not Exist")#查询德克萨斯州
print(f"The city for the state '陕西' is: {city}")#州缩写TX 查询不到德克萨斯州
# =============================================================================
# 语法是 dict.get(key, default=None)
#
# key -- 字典中要查找的键。
#
# default -- 如果指定键的值不存在时,返回该默认值
#
# 这里 key 就是 Texas,意思就是要查找它,然后找不到就返回None
# =============================================================================