打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Learn Python The Hard Way 习题41详解

Learn Python The Hard Way 习题41详解

标签: Python 博客


博主最近在学习Python,看的书是Learn Python The Hard Way(Third Edition), 前40道习题没有什么难度,但是看到习题41的时候,由于出现了很多新函数和新名字以及印刷错误,竟然没看懂这道题的目的。查询了一些函数的用法之后,现在把这道题搞清楚了,分享出来,希望能对其他新手有所帮助。

1、印刷错误

译者:王巍巍
版次:2014年11月第1版
印刷时间:2015年5月北京第3次印刷
具体错误:

#fake class namesfor word in class_names: result = result.replace('%%%', word, 1)# fake other namesfor word in other_names: result = result.replace('***', word, 1)# fake parameter listsfor word in param_names: result = result.replace('@@@', word, 1)results.append(result)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

上面的一段代码在书中是Line47~Line59,这一段代码应该全部再缩进一格,即这段代码处于for sentence in snippet, phrase:的循环中,具体可以参考Exercise 41: Learning To Speak Object Oriented

2、本节新函数介绍

2.1strip()

首次出现位置:Line30
函数原型:str.strip([chars])
参数:chars – 移除字符串头尾指定的字符,默认为空格
返回值:返回移除字符串头尾指定的字符生成的新字符串,移除的是chars中的任意字符
实例:

>>> a = ' 123'>>> print a.strip()123>>> a ='123abc'>>>print a.strip('1cb')23a
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.2capitalize()

首次出现位置:Line34
函数原型:str.capitalize()
参数:None
返回值:返回一个首字母大写,其余字母小写的字符串
实例:

>>> a = 'this is a book called HARRY POTTER'>>> print a.capitalize()This is a book called harry potter
  • 1
  • 2
  • 3

2.3random.sample()

首次出现位置:Line35
函数原型:random.sample(sequence, k)
参数:sequence是一个list,k是一个整数
返回值:返回一个list,该list由sequence中随机的k个元素组成,sequence不变
实例:

>>>list = [1,2,3,4,5,6,7,8,9,10]>>>slice = random.sample(list,5)>>>print slice[3,6,8,2,4]#截取的序列元素并没有顺序
  • 1
  • 2
  • 3
  • 4

2.4count()

首次出现位置:Line35
函数原型:str.count(sub, start= 0,end=len(str))
参数:

  • sub – 搜索的子字符串
  • start – 字符串开始搜索的位置,默认为第一个字符,第一个字符索引值为0
  • end – 字符串中结束搜索的位置,字符中第一个字符的索引为0,默认为字符串的最后一个位置

返回值:返回子字符串在字符串中出现的次数
实例:

>>>a = 'this is a book called HARRY POTTER'>>>print a.count('i')2
  • 1
  • 2
  • 3

2.5join()

首次出现位置:Line42
函数原型:str.join(sequence)
参数:sequence – 要连接元素的list
返回值:返回通过str连接序列中元素后生成的字符串
实例:

>>>str = '-'>>>seq = ['a','b','c']>>>print str.join(seq)a-b-c
  • 1
  • 2
  • 3
  • 4

2.6replace()

首次出现位置:Line49
函数原型:str.replace(old, new[, max])
参数:

  • old – 将被替换的子字符串。
  • new – 新字符串,用于替换old子字符串。
  • max – 可选字符串, 替换不超过 max 次

返回值:返回以new代替old不超过max次的新字符串
实例:

>>>a = 'this is a book called HARRY POTTER'>>>print a.replace('is', 'was', 1)thwas is a book called HARRY POTTER>>>print a.replace('is', 'was')thwas was a book called HARRY POTTER
  • 1
  • 2
  • 3
  • 4
  • 5

2.7keys()

首次出现位置:Line67
函数原型:dict.keys()
参数:None
返回值:返回一个字典所有的键
实例:

>>>dict = {'name':'moverzp', 'age':'23', 'height':'180'}>>>print dict.keys()['age','name','height']
  • 1
  • 2
  • 3

2.8shuffle()

首次出现位置:Line68
函数原型:random.shuffle(lst)
参数:lst – 可以是一个序列或者元组
返回值:None. 直接修改lst的顺序
实例:

>>>list = [1, 2, 3, 4]>>>random.shuffle(list)>>>print list[3, 1, 4, 2]>>>random.shuffle(list)>>>print list[1, 4, 2, 3]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3、代码详解

这段脚本的作用其实就是在设定网页中随机取出单词当做类的名字,函数的名字或者参数的名字。具体操作就是把PHRASES中的”%%%”,”***”,”@@@”替换成随机选择的单词而已,PHRASE_FIRST决定先打印单词替换符号后key-value(键值对)中的key还是value。

分别以流程图和代码注释说明。

  • 流程图如下:
Created with Rapha?l 2.1.0开始从网页读取单词并写入WORDS提取PHRASE中的键存入snippets并打乱顺序从snippets中取出一个元素snippet取出PHRASE中snippet键对应的值phrase(进入convert函数)phrase中有几个“%%%”就随机从WORDS中取几个单词放入class_names将class_names中的单词首字母大写phrase中有几个“***”就随机从WORDS中取几个单词放入other_names将other_names中的单词首字母大写phrase中是否有“@@@”在[1,3]取随机数param_count在WORDS中取param_count个单词,将其用', '连接起来并写入param_namesfor sentence in snippet, phraseresult浅拷贝sentence用class_names中的单词替换result中的“%%%”用other_names中的单词替换result中的“***”用param_names中的单词替换result中的“@@@”把result作为一个元素加入results中snippet和phrase处理完以后,返回results(退出convert函数)根据PHRASE_FIRST的值决定处理后snippet和phrase的打印顺序,即先打印字典中的键还是值结束yesno
  • 代码分析如下:
import randomfrom urllib import urlopenimport sysWORD_URL = 'http://learncodethehardway.org/words.txt'#该网页中全是单独成行的单词WORDS = []PHRASES = { #编写脚本时应该写的代码为key,其解释为value 'class %%%(%%%):': #%%%表示类名 'Make a class named %%% that is-a %%%.', 'class %%%(object):\n\tdef __init__(self, ***)' : 'class %%% has-a __init__ that takes self and *** parameters.', 'class %%%(object):\n\tdef ***(self, @@@)': 'class %%% has-a function named *** that takes self and @@@ parameters.', '*** = %%%()': 'Set *** to an instance of class %%%.', '***.***(@@@)': 'From *** get the *** function, and call it with parameters self, @@@.', '***.*** = '***'': 'From *** get the *** attribute and set it to '***'.'}# do they want to drill phrases firstPHRASE_FIRST = False #False表示先打印key,按下任意键后再打印valueif len(sys.argv) == 2 and sys.argv[1] == 'english': PHRASE_FIRST = True #True表示先打印value,按下任意键后再打印key# load up the words from the websitefor word in urlopen(WORD_URL).readlines(): #一行一行从网页中读取数据 WORDS.append(word.strip()) #删除每一行开始和结尾的空格,只留下单词并加入到words列表中#先从下面的try开始看,调用到该函数时再看回来def convert(snippet, phrase): #我们以传入的具体参数来分析 class_names = [w.capitalize() for w in #从WORDS序列中随机取出1个单词首字母大写后赋值给class_names,此处假设为'Actor' random.sample(WORDS, snippet.count('%%%'))] other_names = random.sample(WORDS, snippet.count('***')) #从WORDS序列中随机取出一个单词赋值给other_names,此处假设为'dinner' results = [] param_names = [] for i in range(0, snippet.count('@@@')): #只循环一次 param_count = random.randint(1,3) #假设随机到了2 param_names.append(', '.join(random.sample(WORDS, param_count))) #从WORDS中随机取出2个单词用逗号和空格连接起来,放在param_names中,假设为'cook, donkey' for sentence in snippet, phrase: #循环两次,只讲解第一次循环 result = sentence[:] #深拷贝,result = 'class %%%(object):\n\tdef ***(self, @@@)' # fake class names for word in class_names: result = result.replace('%%%', word, 1) #result = 'class Actor(object):\n\tdef ***(self, @@@)' # fake other names for word in other_names: result = result.replace('***', word, 1) #result = 'class Actor(object):\n\tdef dinner(self, @@@)' # fake parameter lists for word in param_names: result = result.replace('@@@', word, 1) #result = 'class Actor(object):\n\tdef dinner(self, cook, donkey)' results.append(result) #添加到results序列中 return results #results = ['class Actor(object):\n\tdef dinner(self, cook, donkey)', 'class Actor has-a function named dinner that takes self and cook, donkey parameters.']# keep going until they hit CTRL-Dtry: while True: snippets = PHRASES.keys() #提取PHRASES中的键 random.shuffle(snippets) #打乱snippets中元素(PHRASES中键)顺序 for snippet in snippets: #取一个键来操作,假设取到'class %%%(object):\n\tdef ***(self, @@@)' phrase = PHRASES[snippet] #取出值'class %%% has-a function named *** that takes self and @@@ parameters.' question, answer = convert(snippet, phrase) #现在去上面看这个函数的定义 if PHRASE_FIRST: #先打印value,再打印key question, answer = answer, question print question raw_input('> ') print 'ANSWER: %s\n\n' % answerexcept EOFError: print '\nBye'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

4、总结

  • 当遇到看不懂的代码的时候,可以将其打印出来,用铅笔一行一行写注释去分析
  • 遇到不懂的函数的时候一定要去网上搜索函数原型,至少要了解参数以及返回值
  • 可以用print一步一步来调试
  • 必要的时候画流程图
  • 可以带入具体参数来分析
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
RAKE:快速自动抽取关键词算法
Exercise 41: Learning To Speak Object Oriented
​LeetCode刷题实战186:翻转字符串里的单词 II
C语言实现统计字符串单词数
利用python实现汉字转拼音的2种方法
Document a package using Javadoc - Real's Java How-to
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服