习题39 字典,可爱的字典
2016-07-30 15:33
411 查看
在之前,我们要在列表里面查询什么东西的时候,只能通过数字来查询
比如
things = ['a','b','c','d']
print things[1]
# b
things[1] = 'z'
print things[1]
# z
print things
# ['a','z','c','d']
而字典可以做的就是,让你通过任何东西来找到元素,不只是数字序数什么的了
比如
stuff = {'name':'Zed','age':36,'height':6*12+2}
print stuff['name']
# Zed
print stuff['age']
# 36
print stuff['height']
# 74
stuff['city'] = "San Francisco"
print stuff['city']
# San Francisco
# print stuff[1] is wrong
注意 stuff 是大括号!
以及
print stuff[1]会出错,应该是用了字典就不接受列表玩法了、、、
删除字典里的东西用 del
del stuff['city']
del stuff[1]
del stuff[2]
print stuff
======================================================================================================
接下来是源代码:
# creat a mapping of state to abbreviation(a shortened form of a word or phrase)
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': 'Jacksonviile'
}
# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Porland'
# 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 abbreviation
print '-' * 10
for state, abbrev in states.items():
print "%s has the city %s" % (state,abbrev)
# print every city in states
print '-' * 10
for abbrev, city in cities.items():
print "%s has the city %s" % (abbrev,city)
# now do both at the same time
print '-' * 10
for state, abbrev in states.items():
print "%s state is abbreviated %s and has city %s" % (
state, abbrev, cities[abbrev])
print '-' * 10
# safely get a abbreviation by state that might not be there
state = states.get('Texas',None)
if not state:
print "Sorry, no Texas."
# get a city with a default value
city = cities.get('TX','Does Not Exist')
print "The city for the state 'TX' is: %s" % city
运行结果如下:
Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组
我没有明白的地方是
for state, abbrev in states.items():
这句话 for 到底执行的是什么?
思考了一下,由于在 states 字典里面的格式是
{'state' : 'abbrev'}
所以 for 里面用的也是
for
state, abbrev in ...
这样的格式
Python
字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值
第二个问题: states.get('Texas',None) 有什么用?
语法是 dict.get(key, default=None)
key -- 字典中要查找的键。
default -- 如果指定键的值不存在时,返回该默认值
这里 key 就是 Texas,意思就是要查找它,然后找不到就返回None
相同的,后面从 cities 里面查找 TX ,没有,所以返回 Does Not Exits 到 city 里面
city = cities.get('TX','Does Not Exist')
====================================================================================================
附加练习:
2.
字典是无序的,我不知道怎么检查
但是网上说字典占内存,检索速度超级快
比如
things = ['a','b','c','d']
print things[1]
# b
things[1] = 'z'
print things[1]
# z
print things
# ['a','z','c','d']
而字典可以做的就是,让你通过任何东西来找到元素,不只是数字序数什么的了
比如
stuff = {'name':'Zed','age':36,'height':6*12+2}
print stuff['name']
# Zed
print stuff['age']
# 36
print stuff['height']
# 74
stuff['city'] = "San Francisco"
print stuff['city']
# San Francisco
# print stuff[1] is wrong
注意 stuff 是大括号!
以及
print stuff[1]会出错,应该是用了字典就不接受列表玩法了、、、
删除字典里的东西用 del
del stuff['city']
del stuff[1]
del stuff[2]
print stuff
======================================================================================================
接下来是源代码:
# creat a mapping of state to abbreviation(a shortened form of a word or phrase)
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': 'Jacksonviile'
}
# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Porland'
# 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 abbreviation
print '-' * 10
for state, abbrev in states.items():
print "%s has the city %s" % (state,abbrev)
# print every city in states
print '-' * 10
for abbrev, city in cities.items():
print "%s has the city %s" % (abbrev,city)
# now do both at the same time
print '-' * 10
for state, abbrev in states.items():
print "%s state is abbreviated %s and has city %s" % (
state, abbrev, cities[abbrev])
print '-' * 10
# safely get a abbreviation by state that might not be there
state = states.get('Texas',None)
if not state:
print "Sorry, no Texas."
# get a city with a default value
city = cities.get('TX','Does Not Exist')
print "The city for the state 'TX' is: %s" % city
运行结果如下:
Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组
我没有明白的地方是
for state, abbrev in states.items():
这句话 for 到底执行的是什么?
思考了一下,由于在 states 字典里面的格式是
{'state' : 'abbrev'}
所以 for 里面用的也是
for
state, abbrev in ...
这样的格式
Python
字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值
第二个问题: states.get('Texas',None) 有什么用?
语法是 dict.get(key, default=None)
key -- 字典中要查找的键。
default -- 如果指定键的值不存在时,返回该默认值
这里 key 就是 Texas,意思就是要查找它,然后找不到就返回None
相同的,后面从 cities 里面查找 TX ,没有,所以返回 Does Not Exits 到 city 里面
city = cities.get('TX','Does Not Exist')
====================================================================================================
附加练习:
2.
字典是无序的,我不知道怎么检查
但是网上说字典占内存,检索速度超级快
相关文章推荐
- 笨方法学Python 习题 39: 字典, 可爱的字典
- 笨方法学习Python-习题40: 字典, 可爱的字典
- 笨方法学python 习题39(列表)习题40(字典)
- Learn Python The Hard Way学习(39) - 可爱的字典
- Exercise 39:字典,噢 ,可爱的字典
- 小甲鱼python教程第25讲--python字典 笔记及习题答案
- 字典(习题)
- 可爱的python课后习题(三)
- 字典 习题
- 可爱的python课后习题(二)
- <Python>字典,可爱的字典
- CDays-3 习题二 (字典及文件读取练习)及相关内容解析。Python 基础教程
- PyThon--8-6 习题(列表,字典)
- 可爱的python习题(四)
- 《C++ Primer》第五版课后习题解答_第二章(5)(39-42)
- Chapter6课后习题 字典(第三周Ⅱ)
- 习题3—39
- 一月十日练习习题,1数组数据存入字典并输出2降序 和倒叙 输出数组中内容3对字符串当中信息进行查找是否存在4 把数组当中信息按照中文排序
- 可爱的python课后习题
- 习题10-39 UVA 11186 Circum Triangle圆周上的三角形