笔记31 笨办法学python练习39可爱的词典

从列表进到字典这个数据结构了。交互式的那个py编译器还需要继续熟悉,很多都是要记忆得很清楚。到atom编辑器上打码,还是错误多多,好在经过多轮练习,现在寻找错误的水平提高了不少。昨天晚上未能将错误编码一网打尽,今天上午继续开键,很快就将错误全扫,其实大都属于打印不细致造成。按照ps提示和对于代码的理解,一般不会很困难了,这让人有点欣慰感。
现在来对代码的思路做一点整理和扩展,做成ex39.1.py,对字典的理解就会更好一些。把对美国州的对应换成中国省的对应,看看这样转换的结果如何。
终于做成功了,用中文的地名作为词典内容,稍有更改,做成ex39.1.py。改错是个技巧活,有时候会进到一个误区,以为既然要求出城市名,自然是城市的集合,但因为你用州来作为变量来取值城市,实际上还是用州来表示城市名,打印出来的则是城市。
例如
rint("浙江 has: ", cities[states[‘杭州’]])
print("四川 has: ", cities[states[‘成都’]])
像这样的错误往往隐藏着,一不小心就被蒙住了以为想当然无措。但走投无路之时你会猛地想到,是不是这样的地方有名称错误,结果果然有错。后面69行出现的整体错误integers,关键字错误key,还有语法错误syntax也是迷惑人的,估计熟悉后要好一些吧。
转换练习xe39.1.py

在这里插入代码片# create a mapping of state to abbreviation
states = {'湖北': '鄂','广东': '粤','浙江': '浙','四川': '川','湖南': '湘',
}# create a basic set of states and some cities in them
cities = {'鄂': '武汉','粤': '广州','浙': '杭州','川': '成都'
}# add some more cities
cities['粤'] = '深圳'
cities['湘'] = '长沙'# create a mapping of cities to abbreviation
cities_abbrev = {'武汉': '汉','广州': '穗','杭州': '杭','成都': '成','深圳': '深','长沙': '长','北京': '京','上海': '沪'
}# print some cities
print('-' * 10)
print("粤 State has: ", cities['粤'])
print("湘 State has: ", cities['湘'])# print some states
print('-' * 10)
print("湖北's abbreviation is: ", states['湖北'])
print("广东's abbreviation is: ", states['广东'])# do it by using the stwte then cities dict
print('-' * 10)
print("浙江 has: ", cities[states['浙江']])
print("四川 has: ", cities[states['四川']])# print every stste abbrev
print('-' * 10)
for state, abbrev in list(states.items()):print(f"{state} is abbreviated{abbrev}")#print every city in state
print('-' * 10)
for abbrev, city in list(cities_abbrev.items()):print(f"{abbrev} has the city {city}")# now do both at the same time
print('-' * 10)
for state, abbrev in list(states.items()):print(f"{state} is abbreviated{abbrev}")print(f"and has city {cities[abbrev]}")# print every city and  their abbrev
print('-' * 10)
for cities_abbrev, abbrev in list(cities_abbrev.items()):print(f"{city} is abbreviated{abbrev}")print(f"and has city {cities_abbrev}")if not city:print("Sorry, no this.")# get a city with a default value
city = cities.get('TX', 'Does Not Exit')
print(f"The city for the state '桂' is: {city}")

执行

在这里插入代码片SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex39.1.py
----------
粤 State has:  深圳
湘 State has:  长沙
----------
湖北's abbreviation is:  鄂
广东's abbreviation is:  粤
----------
浙江 has:  杭州
四川 has:  成都
----------
湖北 is abbreviated鄂
广东 is abbreviated粤
浙江 is abbreviated浙
四川 is abbreviated川
湖南 is abbreviated湘
----------
武汉 has the city 汉
广州 has the city 穗
杭州 has the city 杭
成都 has the city 成
深圳 has the city 深
长沙 has the city 长
北京 has the city 京
上海 has the city 沪
----------
湖北 is abbreviated鄂
and has city 武汉
广东 is abbreviated粤
and has city 深圳
浙江 is abbreviated浙
and has city 杭州
四川 is abbreviated川
and has city 成都
湖南 is abbreviated湘
and has city 长沙
----------
沪 is abbreviated汉
and has city 武汉
沪 is abbreviated穗
and has city 广州
沪 is abbreviated杭
and has city 杭州
沪 is abbreviated成
and has city 成都
沪 is abbreviated深
and has city 深圳
沪 is abbreviated长
and has city 长沙
沪 is abbreviated京
and has city 北京
沪 is abbreviated沪
and has city 上海
The city for the state '桂' is: Does Not Exit
PS C:\Users\lenovo\1pythonw>以上还是有错,继续改错得ex39.2.py```python
在这里插入代码片# create a mapping of state to abbreviation
states = {'湖北': '鄂','广东': '粤','浙江': '浙','四川': '川','湖南': '湘',
}# create a basic set of states and some cities in them
cities = {'鄂': '武汉','粤': '广州','浙': '杭州','川': '成都'
}# add some more cities
cities['粤'] = '深圳'
cities['湘'] = '长沙'# create a mapping of cities to abbreviation
cities_abbrev = {'武汉': '汉','广州': '穗','杭州': '杭','成都': '成','深圳': '深','长沙': '长','北京': '京','上海': '沪'
}# print some cities
print('-' * 10)
print("粤 State has: ", cities['粤'])
print("湘 State has: ", cities['湘'])# print some states
print('-' * 10)
print("湖北's abbreviation is: ", states['湖北'])
print("广东's abbreviation is: ", states['广东'])# do it by using the stwte then cities dict
print('-' * 10)
print("浙江 has: ", cities[states['浙江']])
print("四川 has: ", cities[states['四川']])# print every stste abbrev
print('-' * 10)
for state, abbrev in list(states.items()):print(f"{state} is abbreviated{abbrev}")#print every city in state
print('-' * 10)
for abbrev, city in list(cities_abbrev.items()):print(f"{abbrev} has abbreviation name {city}")# now do both at the same time
print('-' * 10)
for state, abbrev in list(states.items()):print(f"{state} is abbreviated{abbrev}")print(f"and has city {cities[abbrev]}")if not city:print("Sorry, no this.")# get a city with a default value
city = cities.get('TX', 'Does Not Exit')
print(f"The city for the state '桂' is: {city}")

执行

在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex39.2.py
----------
粤 State has:  深圳
湘 State has:  长沙
----------
湖北's abbreviation is:  鄂
广东's abbreviation is:  粤
----------
浙江 has:  杭州
四川 has:  成都
----------
湖北 is abbreviated鄂
广东 is abbreviated粤
浙江 is abbreviated浙
四川 is abbreviated川
湖南 is abbreviated湘
----------
武汉 has abbreviation name 汉
广州 has abbreviation name 穗
杭州 has abbreviation name 杭
成都 has abbreviation name 成
深圳 has abbreviation name 深
长沙 has abbreviation name 长
北京 has abbreviation name 京
上海 has abbreviation name 沪
----------
湖北 is abbreviated鄂
and has city 武汉
广东 is abbreviated粤
and has city 深圳
浙江 is abbreviated浙
and has city 杭州
四川 is abbreviated川
and has city 成都
湖南 is abbreviated湘
and has city 长沙
The city for the state '桂' is: Does Not Exit
PS C:\Users\lenovo\1pythonw>

笔记31 笨办法学python练习39可爱的词典相关推荐

  1. 笔记33 笨办法学python练习40之二:类和对象

    笔记33 笨办法学python练习40之二:类和对象 类和模块差不多么?把这个练习继续往下做,看是否真有如此结果.刚刚对模块有了点感觉,加上这个类class也是要创建,但这个练习所创建的class M ...

  2. 笔记37 笨办法学python练习43面向对象OOP的游戏代码(二)代码的反复理解

    笔记37 笨办法学python练习43面向对象OOP的游戏代码(二)代码的反复理解 连续贴着这个练习43的代码折腾了整整两天,把那些英文文本翻译为中文文本,重新装进这个代码之中.本想一段一段的运行,发 ...

  3. 笔记36 笨办法学python练习43面向对象OOP的文字理解(一)

    笔记36 笨办法学python练习43面向对象OOP的文字理解(一) 先仔细看了本练习的文本,感到一个笔记写不下来这个复杂的练习过程,那就先把文字理解的部分先来完成,再用一篇笔记来完成代码的理解. 一 ...

  4. 笔记35 笨办法学python练习42对象、类、从属关系和部件关系

    笔记35 笨办法学python练习42对象.类.从属关系和部件关系 一.类.对象与从属关系 这个练习是一个有点哲学意味的练习,讨论对象和类的关系,也就是从属关系.对象不就是下属于某个类的子类,或者个体 ...

  5. 笨办法学python 粗略笔记(learn python the hard way)

    笨办法学python 粗略笔记(learn python the hard way) 标签(空格分隔): python # _*_ coding: utf_8 _*_ ''' ### ex1 prin ...

  6. 笨办法学python第五版_最新《笨办法学python》学习笔记

    <笨办法学 python >学习笔记( Python 3.6 ) 习题 19 ex19.py # -*- coding: utf-8 -*- # 定义 cheese_and_cracker ...

  7. 笨办法学Python 3 ex35学习笔记

    笨办法学Python 3 ex35学习笔记 from sys import exitdef gold_room():#print("This room is full of gold. Ho ...

  8. 笨办法学Python——学习笔记1

        最近想学gtk,但是gtk在window上编译和运行挺慢的,于是搜索了一下发现了pygtk.在前几天 把环境都配好了,现在想同时学gtk和pygtk,但Python没学过,找到了<笨办法 ...

  9. 计算机编程书籍-笨办法学Python 3:基础篇+进阶篇

    编辑推荐: 适读人群 :本书适合所有已经开始使用Python的技术人员,包括初级开发人员和已经升级到Python 3.6版本以上的经验丰富的Python程序员. "笨办法学"系列, ...

  10. python教程第四版pdf下载-笨办法学python第四版

    笨办法学python第四版是由Zed Shaw所编写的一本书.如果你还是Python新手,那么这是一本非常不错的入门书籍.书本里以习题方式,引导读者慢慢学会了编程. 目录: 习题 0: 准备工作 习题 ...

最新文章

  1. ArcEngine数据删除几种方法和性能比较
  2. sbt+Scala IDE建立Scala项目
  3. jQuery是否可以获取与元素关联的所有CSS样式?
  4. AOP 中必须明白的概念-通知(Advice)
  5. Exceptions(小节)
  6. C#编程中的66个好习惯,你有多少个?(转)
  7. LeetCode 1894. 找到需要补充粉笔的学生编号
  8. SQL Server创建Job, 实现执行相同脚本而产生不同作业计划的探究
  9. nginx php value,PHP+NGINX参数优化
  10. 上传 mp4 格式判断_视频如何转换成通用的MP4格式?按下这个键,10秒就能搞定...
  11. nhibernate mysql配置_NHibernate各种数据库连接参数文件配置方法说明
  12. 使用Git将本地项目上传到Github操作详解
  13. 计算机二级考试基础知识文档,计算机二级公共基础知识(考试必考)
  14. 关于UIAlertAction如何修改sheet上的字体颜色
  15. dd 删除引导扇区_硬盘U盘数据怎么用bootice彻底删除及清零引导记录教程
  16. 【大气红歌】著名民通歌唱家拉齐的音乐之路
  17. ue4风格化材质_在UE4中制作风格化场景:Bird House_资源库
  18. 为啥外包喜欢php,为什么要面向对象?
  19. 圣诞之歌:クリスマス タイム和My Baby Grand~ぬくもりが欲しくて~ ZARD
  20. 【系统分析师之路】计算机组成原理章节错题集锦

热门文章

  1. Delphi基础教程图文版之语句(循环语句)
  2. win7如何添加终端服务器,Win7系统如何添加超级终端?Windows7系统超级终端的添加方法...
  3. java cxf调用webservice_Java调用WebService方法总结(7)--CXF调用WebService
  4. 腾达Tenda 路由器后门分析
  5. 高等数学(第七版)同济大学 习题3-4 个人解答(前8题)
  6. 《卓有成效的管理者》读书笔记
  7. 实况足球2015pc版
  8. 中国各省份矢量地图-可编辑
  9. html固定广告位置,创建固定位置弹出浮动广告的实例代码
  10. 开源软件清除了“开源”和“商业”之间的障碍——商业软件、开源软件和自由软件的区别