设为首页 收藏本站
查看: 1203|回复: 0

[经验分享] Learn Python The Hard Way学习(39)

[复制链接]

尚未签到

发表于 2017-5-6 10:47:50 | 显示全部楼层 |阅读模式
下面我们学习另外一种常用的容器:字典。



python叫dict,有的语言叫hash,我都会使用到,这些都不是重点,重点是它和list的区别。我们这样使用list:

>>> things = ['a', 'b', 'c', 'd']

>>> print things[1]

b

>>> things[1] = 'z'

>>> print things[1]

z

>>> print things

['a', 'z', 'c', 'd']






我们可以在list中使用索引,就是说我们可以通过数字找到list中的元素。




而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[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, 'height': 74}

>>>






当然,字典不可能只可以插入数据,可以删除数据:

>>> 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 same 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






运行结果

root@he-desktop:~/mystuff# 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. 在python文档中找到字典的介绍,看看还能做些什么




3. 看看有什么是字典不能做的,主要是字典是无序的。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373713-1-1.html 上篇帖子: 整理网上python解析xml文件相关资料汇总 下篇帖子: Learn Python The Hard Way学习(37)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表