首页 > 其他 > 详细

Exercise 39: Dictionaries, Oh Lovely Dictionaries

时间:2014-10-26 12:57:56      阅读:325      评论:0      收藏:0      [点我收藏+]
# create a mapping of state to abbreviation
states = {
‘Oregon‘: ‘OR‘,
‘Florida‘: ‘FL‘,
‘California‘: ‘CA‘,
‘New York‘: ‘NY‘,
‘Michigan‘: ‘MI‘ }
# create a basic set of states and some cities in them
cities = {
‘CA‘: ‘San Francisco‘,
‘MI‘: ‘Detroit‘,
‘FL‘: ‘Jacksonville‘ }
# add some more cities
cities[‘NY‘] = ‘New York‘
cities[‘OR‘] = ‘Portland‘
# print out some cities
print ‘-‘ * 10
print "NY State has: ", cities[‘NY‘]
print "OR State has: ", cities[‘OR‘]
# print some states
print ‘-‘ * 10
print "Michigan‘s abbreviation is: ", states[‘Michigan‘]
print "Florida‘s abbreviation is: ", states[‘Florida‘]
# do it by using the state then cities dict
print ‘-‘ * 10
print "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

原文:http://www.cnblogs.com/hluo/p/4051794.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!