您的位置:首页 > 编程语言 > Python开发

Exercise 39:字典,噢 ,可爱的字典

2014-04-16 00:24 323 查看
原文链接:http://learnpythonthehardway.org/book/ex39.html

       现在我要再教你一种让你伤脑筋的容器类型的数据结构了,一旦你学会了容器,你将拥有超酷的能力。这是最有用的容器:字典(dictionary)。

       Python中称其为“dict”,其它语言称它们为“hash”。两种名称我都会用到,不过这并不重要,重要的是它和列表的区别。你看,一个列表可以让你可以像这样操作:

>>> print things[1]
b
>>> things[1] = 'z'
>>> print things[1]
z
>>> print things
['a', 'z', 'c', 'd']
>>>
       在一个列表中你可以直接使用数字作为 " 索引" ,这也就是说你可以直接用索引数值找到列表中对应的元素。到现在你应该已经知道了关于列表的这个作用 ,但是你要知道你只能通过数字才能获取一个列表中元素。

       但是 dict 中可以让你使用任何类型的索引值,不仅仅是数字。是的,dict 可以将一件东西和另一件东西联系起来,不过它们是什么类型。让我们来看一看:

>>> 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
>>>
       可以看到可以我们可以使用字符串来替代数字来从stuff字典中获取我们想要的元素。我们也可以使用字符串在字典中放入新的元素。也不是必须得用字符串,也可以像下面这样操作:

>>> stuff[1] = "Wow"
>>> stuff[2] = "Neato"
>>> print stuff[1]
Wow
>>> print stuff[2]
Neato
>>> print stuff
{'city': 'San Francisco', 2: 'Neato', 'name': 'Zed', 1: 'Wow', 'age': 36, 'heigh
t': 74}
>>>
       在上面这个代码里面我直接使用了数字,你可以看到我打印出来的结果显示数字是作为相应字符串的键值。当然我也可以使用任何类型数据,额,是几乎可以但是在目前我们就加速可以使用任何类型吧。

       当然,字典只能放入元素是非常愚蠢的,所以这里告诉怎么使用 del 关键字去删除字典中的元素:
>>> del stuff['city']
>>> del stuff[1]
>>> del stuff[2]
>>> stuff
{'name': 'Zed', 'age': 36, 'height': 74}       下面我们将要做的练习你必须非常仔细的去研究。我希望你手动输入这个练习并且试着理解它做了什么事情。当我在字典中添加元素、获取元素以及我在这里使用的各种操作时你都做好标记。
# 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 abbreviation
print '-' * 10
for state, abbrev in states.items():
print "%s is abbreviated %s" % (state ,abbrev)

# print every city in state
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

输出结果如下:

E:\>python ex39.py
----------
NY State has: New York
OR State has: Portland
----------
Michigan's abbreviation is: MI
Florida's abbreviation is: FL
----------
Michigan has: Detroit
Florida has: Jacksonville
----------
California is abbreviated CA
Michigan is abbreviated MI
New York is abbreviated NY
Florida is abbreviated FL
Oregon is abbreviated OR
----------
FL has the city Jacksonville
CA has the city San Francisco
MI has the city Detroit
OR has the city Portland
NY has the city New York
----------
California state is abbreviated CA and has city San Francisco
Michigan state is abbreviated MI and has city Detroit
New York state is abbreviated NY and has city New York
Florida state is abbreviated FL and has city Jacksonville
Oregon state is abbreviated OR and has city Portland
----------
Sorry, no Texas.
The city for the state 'TX' is: Does Not Exist

研究训练:

1、写出关于你所在的国家或者一些其他国家中类似上述这种州/区域与城市之间的映射关系。

2、去查找更多关于字典(或者称 dicts ,dict)的介绍文档并且做更多关于字典的练习。

3、找出你用字典不能做的事情。一个很明显的事情就是字典中的元素是无序的,你可以试着验证看看是不是。

学生遇见的常见问题:

字典和列表之间有什么不同?

答:列表中的元素是有序的,而字典中是一些项(称为”键值“)和其他项相匹配(称为”值“)。

我可以用字典来做什么?

答:任何时候你想要得到一个值你必须先”查找“对应的键值。实际上你可以调用字典去做”查找表“操作。

我可以用列表来做什么?

答:列表是一个有序序列,你只能通过数值索引来找到需要的项。

如果我需要使用字典,同时也需要其实有序的怎么办?

答:你可以看一下 Python 中的collections.OrderedDict 数据结构。网上搜索关于它的介绍文档。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python 字典