Published 9月 19, 2018 by with 0 comment

習題39 - 字典, 可愛的字典




用Notepad++打下列的程式碼,
另存為ex39.py. 我附上中文注釋方便好讀.
# 建立一個字典(states), 注意到每行後面有個逗號.
# create a mapping of state to abbreviation
states = {
    'Oregon': 'OR',
    'Florida': 'FL',
    'California': 'CA',
    'New York': 'NY',
    'Michigan': 'MI'
}

# 建立一個字典(cities)
# Create a basic set of states and some cities in them
cities = {
    'CA': 'San Francisco',
    'MI': 'Detorit',
    'FL': 'Jacksonville'
}

# 將新元素加入字典(cities)
# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'

# 查詢字典(cities),給出鍵查值.
# print out some cities
print('-' * 10)
print("NY State has: ", cities['NY'])
print("OR state has: ", cities['OR'])

# 查詢字典(states),給出鍵查值.
# print some states
print('-' * 10)
print("Michigan's abbreviation is: ", states['Michigan'])
print("Florida's abbreviation is: ", states["Florida"])

# 這裡做了兩件事, 先查詢字典(states),給出鍵查值.
# 然後再用該值設為鍵去查詢字典(cities)
# do it by using the state then cities dict
print('-' * 10)
print("Michigan has: ", cities[states['Michigan']])
print("Florida has: ", cities[states['Florida']])

# items: 是以列表的方式返回出(鍵, 值)
# 所以 state 跟 abbrev 就是字典(states)的值
# print every state abbreviation
print('-' * 10)
for state, abbrev in list(states.items()):
    print(f"{state} is abbreviated {abbrev}")

# items: 是以列表的方式返回出(鍵, 值)
# 所以 abbrev 跟 city 就是字典(cities)的值
# print every city in state
print('-' * 10)
for abbrev, city in list(cities.items()):
    print(f"{abbrev} has the city {city}")

# items: 是以列表的方式返回出(鍵, 值)
# 所以 state 跟 abbrev 就是字典(states)的值
# 用格式化的方式展示字典(states)的值
# now do both at the same time
print('-' * 10)
for state, abbrev in list(states.items()):
    print(f"{state} state is abbreviated {abbrev}")
    print(f"and has city {cities[abbrev]}")

# get: 是返回字典中指定鍵的值
print('-' * 10)
# safely get a abvreviation by state that might not be there
state = states.get('Texas')

if not state:
    print("Sorry, no Texas.")

# get: 是返回字典中指定鍵的值, 
# 第二個是設定不在字典時的默認值(default = None)
# get a city with a default vaule
city = cities.get('TX', 'Does not Exist')
print(f"The city for the state 'TX' is: {city}")

然後用Windows的cmd, 執行python.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW
C:\Users\Peter\Desktop\Python\LP3THW>python ex39.py
----------
NY State has:  New York
OR state has:  Portland
----------
Michigan's abbreviation is:  MI
Florida's abbreviation is:  FL
----------
Michigan has:  Detorit
Florida has:  Jacksonville
----------
Oregon is abbreviated OR
Florida is abbreviated FL
California is abbreviated CA
New York is abbreviated NY
Michigan is abbreviated MI
----------
CA has the city San Francisco
MI has the city Detorit
FL has the city Jacksonville
NY has the city New York
OR has the city Portland
----------
Oregon state is abbreviated OR
and has city Portland
Florida state is abbreviated FL
and has city Jacksonville
California state is abbreviated CA
and has city San Francisco
New York state is abbreviated NY
and has city New York
Michigan state is abbreviated MI
and has city Detorit
----------
Sorry, no Texas.
The city for the state 'TX' is: Does not Exist

C:\Users\Peter\Desktop\Python\LP3THW>

完成


最初發表 / 最後更新: 2018.09.19 / 2018.09.19

0 comments:

張貼留言