笨方法学Python 习题 38: 列表的操作
来源:互联网 发布:wp 利用 知乎 编辑:程序博客网 时间:2024/06/05 04:23
#!usr/bin/python# -*-coding:utf-8-*-states = {"Oregon":"OR","Floriad":"FL","California":"CA","New York":"NY","Michigan":"MI"}cities = {"CA":"San Francisco","MI":"Detroit","FL":"Jacksonville"}cities["NY"] = "New York"cities["OR"] = "Portland"print("_"*10)print("NY State has:",cities["NY"])print("OR State has:",cities["OR"])#NY State has: New York#OR State has: Portlandprint("-"*10)print("Michigan's abbreviation is: ", states['Michigan'])print("Florida's abbreviation is: ", states['Floriad'])#Michigan's abbreviation is: MI#Florida's abbreviation is: FLprint('-' * 10)print("Michigan has: ", cities[states['Michigan']])print("Floriad has: ", cities[states['Floriad']])#Michigan has: Detroit#Florida has: Jacksonvilleprint("-"*10)for state , abbrev in states.items():print("%s is abbreviated %s"%(state,abbrev))# items() 函数以列表返回可遍历的(键, 值) 元组数组。#California is abbreviated CA#Michigan is abbreviated MI#New York is abbreviated NY#Florida is abbreviated FL#Oregon is abbreviated ORprint("-"*10)for abbrev , city in cities.items():print("%s has the city %s"%(abbrev,city))#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 Yorkprint("-"*10)for state , abbrev in states.items():print("%s state is abbreviated %s and has city %s"%(state,abbrev,cities[abbrev]))#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 Portlandprint("-"*10)state = states.get("Texas",None)#get() 函数返回指定键的值,如果值不在字典中返回默认值。if not state: print("Sorry, no Texas.")city = cities.get("TX","Does Not Exist")print("The city for the state 'TX' is:%s"%city)
运行结果如下:
----------NY State has: New YorkOR State has: Portland----------Michigan's abbreviation is: MIFlorida's abbreviation is: FL----------Michigan has: DetroitFlorida has: Jacksonville----------California is abbreviated CAMichigan is abbreviated MINew York is abbreviated NYFlorida is abbreviated FLOregon is abbreviated OR----------FL has the city JacksonvilleCA has the city San FranciscoMI has the city DetroitOR has the city PortlandNY has the city New York----------California state is abbreviated CA and has city San FranciscoMichigan state is abbreviated MI and has city DetroitNew York state is abbreviated NY and has city New YorkFlorida state is abbreviated FL and has city JacksonvilleOregon state is abbreviated OR and has city Portland----------Sorry, no Texas.The city for the state 'TX' is: Does Not Exist
加分习题
1、将每一个被调用的函数以上述的方式翻译成 Python 实际执行的动作。例如: ' '.join(things) 其实是 join(' ', things) 。
2、将这两种方式翻译为自然语言。例如, ' '.join(things) 可以翻译成“用 ‘ ‘ 连接(join) things”,而 join(' ', things) 的意思是“为 ‘ ‘ 和 things 调用 join 函数”。这其实是同一件事情。
3、上网阅读一些关于“面向对象编程(Object Oriented Programming)”的资料。晕了吧?嗯,我以前也是。别担心。你将从这本书学到足够用的关于面向对象编程的基础知识,而以后你还可以慢慢学到更多。
4、查一下 Python中的 “class” 是什么东西。不要阅读关于其他语言的 “class” 的用法,这会让你更糊涂。
dir(something) 和 something 的 class 有什么关系?
5、如果你不知道我讲的是些什么东西,别担心。程序员为了显得自己聪明,于是就发明了 Object Oriented Programming,简称为 OOP,然后他们就开始滥用这个东西了。如果你觉得这东西太难,你可以开始学一下 “函数编程(functional programming)”。
常见问题回答
你不是说别用 while-loop 吗?
是的。你要记住,有时候如果你有很好的理由,那么规则也是可以打破的。死守着规则不放的人是白痴。
stuff[3:5] 实现了什么功能?
这是一个列表切片动作,它会从 stuff 列表的第 3 个元素开始取值,直到第 5 个元素。注意,这里并不包含第 5 个元素,这跟 range(3,5) 的情况是一样的。
为什么 join(' ', stuff) 不灵?
join 的文档写得有问题。其实它不是这么工作的,其实它是你要插入的字符串的一个方法函数,函数的参数是你要连接的字符串构成的数组,所以应该写作 ' '.join(stuff) 。
- 笨方法学Python 习题 38: 列表的操作
- 笨方法学Python 习题 34: 访问列表的元素
- 笨方法学习Python-习题39: 列表的操作
- 笨方法学Python 习题 32: 循环和列表
- 笨方法学Python 习题 17: 更多文件操作
- 笨方法学python 习题32(循环和列表)习题33(while循环)
- 笨方法学python 习题34(访问列表元素)习题35(作死小游戏)
- 笨方法学python 习题39(列表)习题40(字典)
- 列表的操作,习题38,learn python the hard way.
- 习题38 列表的操作
- 笨方法学Python 习题 25: 更多更多的练习
- 笨方法学Python 习题 39: 字典, 可爱的字典
- 《笨方法学python》习题43的学习笔记
- 笨方法学习Python-习题34: 访问列表的元素
- 笨方法学python 习题35修改
- 笨方法学Python 习题 11: 提问
- 笨方法学Python 习题 41: 物以类聚
- 笨方法学Python 习题0
- View、Window、WindowManager与WMS之间的关系
- 实习笔记之nexus maven私服搭建
- DOM中获取元素的方法
- java-文件操作RandomAccessFile
- Organopolysilazane
- 笨方法学Python 习题 38: 列表的操作
- 高通滤波与低通滤波的简单理解
- HDFS使用API操作文件权限
- 使用java的过滤器filter备忘(HttpServletResponseWrapper)
- hive的数据倾斜解决策略
- 网易校招
- 一个运动控制系统的实例
- 半球贴180度全景图片
- CentOs6.5下webbench的安装及测试