首页 > 代码库 > Exercise 39: Dictionaries, Oh Lovely Dictionaries
Exercise 39: Dictionaries, Oh Lovely Dictionaries
# create a mapping of state to abbreviationstates = {‘Oregon‘: ‘OR‘,‘Florida‘: ‘FL‘,‘California‘: ‘CA‘,‘New York‘: ‘NY‘,‘Michigan‘: ‘MI‘ }# create a basic set of states and some cities in themcities = {‘CA‘: ‘San Francisco‘,‘MI‘: ‘Detroit‘,‘FL‘: ‘Jacksonville‘ }# add some more citiescities[‘NY‘] = ‘New York‘cities[‘OR‘] = ‘Portland‘# print out some citiesprint ‘-‘ * 10print "NY State has: ", cities[‘NY‘]print "OR State has: ", cities[‘OR‘]# print some statesprint ‘-‘ * 10print "Michigan‘s abbreviation is: ", states[‘Michigan‘]print "Florida‘s abbreviation is: ", states[‘Florida‘]# do it by using the state then cities dictprint ‘-‘ * 10print "Michigan has: ", cities[states[‘Michigan‘]]print "Florida has: ", cities[states[‘Florida‘]]# print every state abbreviat
a = dict.get(‘A‘, ‘Default‘)
if ‘A‘ exists, print the value of ‘A‘; else print default value.
Exercise 39: Dictionaries, Oh Lovely Dictionaries
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。