一 相关知识
1 split()方法
str.split(str="", num=string.count(str))
2 sorted()函数
语法:
sorted(iterable, cmp=None, key=None, reverse=False)
3 pop()函数
(1)列表:
list.pop(obj=list[-1])
(2)字典
pop(key[,default])
二 代码及执行结果
1 代码:ex25.py文件
1 def break_words(stuff): 2 """This function will break up words for us.""" 3 words = stuff.split(‘ ‘) # 调用split函数利用空格将变量stuf分片,并返回分片后的字符串列表 4 return words 5 6 def sort_words(words): 7 """Sorts the words.""" 8 return sorted(words) # 对所有可迭代对象进行排序操作,并返回一个新的排序后的列表 9 10 def print_first_word(words): 11 """Prints the first word after popping it off.""" 12 word = words.pop(0) # 删除列表第一个元素,并将其返回值赋给变量word 13 print(word) 14 15 def print_last_word(words): 16 """Print the last word after popping it off.""" 17 word = words.pop(-1) # 删除列表最后一个元素,并将其返回值赋给变量word 18 print(word) 19 20 def sort_sentence(sentence): 21 """Takes in a full sentence and returns the sorted words.""" 22 words = break_words(sentence) # 调用我们定义的break_words函数将sentences长字符串分片后返回相应列表,并将该列表赋值给变量words 23 return sort_words(words) # 调用sort_words给得到的列表排序,并作为函数返回值传出 24 25 def print_first_and_last(sentence): 26 """Print the first and last words of the sentence.""" 27 words = break_words(sentence) 28 print_first_word(words) # 调用print_first_word函数打印句子sentences的第一个单词 29 print_last_word(words) # 调用print_last_word函数打印句子sentences的最后一个单词 30 31 def print_first_and_last_sorted(sentence): 32 """Sorts the words then prints the first and last one.""" 33 words = sort_sentence(sentence) 34 print_first_word(words) # 打印排序后得到列表的第一个单词 35 print_last_word(words) # 打印排序后得到列表的最后一个单词
2 执行结果
PS E:\3_work\4_python\2_code_python\02_LearnPythonTheHardWay> python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import ex25 >>> sentence = "All good things come to those who wait." >>> words = ex25.break_words(sentence) >>> words [‘All‘, ‘good‘, ‘things‘, ‘come‘, ‘to‘, ‘those‘, ‘who‘, ‘wait.‘] >>> sorted_words = ex25.sort_words(words) >>> sorted_words [‘All‘, ‘come‘, ‘good‘, ‘things‘, ‘those‘, ‘to‘, ‘wait.‘, ‘who‘] >>> ex25.print_first_word(words) All >>> ex25.print_last_word(words) wait. >>> words [‘good‘, ‘things‘, ‘come‘, ‘to‘, ‘those‘, ‘who‘] >>> ex25.print_first_word(sorted_words) All >>> ex25.print_last_word(sorted_words) who >>> sorted_words [‘come‘, ‘good‘, ‘things‘, ‘those‘, ‘to‘, ‘wait.‘] >>> sorted_words = ex25.sort_sentence(sentence) >>> sorted_words [‘All‘, ‘come‘, ‘good‘, ‘things‘, ‘those‘, ‘to‘, ‘wait.‘, ‘who‘] >>> ex25.print_first_and_last(sentence) All wait. >>> ex25.print_first_and_last_sorted(sentence) All who >>> help(ex25) Help on module ex25: NAME ex25 FUNCTIONS break_words(stuff) This function will break up words for us. print_first_and_last(sentence) Print the first and last words of the sentence. print_first_and_last_sorted(sentence) Sorts the words then prints the first and last one. print_first_word(words) Prints the first word after popping it off. print_last_word(words) Print the last word after popping it off. sort_sentence(sentence) Takes in a full sentence and returns the sorted words. sort_words(words) Sorts the words. FILE e:\3_work\4_python\2_code_python\02_learnpythonthehardway\ex25.py >>> help(ex25.break_words) Help on function break_words in module ex25: break_words(stuff) This function will break up words for us. >>>
注意:help(ex25)和help(ex25.break_words)是用来查看程序的帮助文档的,它会把程序中三引号引起来的内容——即文档字符串的内容打印出来形成一个新的说明文档,具体可见我写的另外一个博客:https://www.cnblogs.com/luoxun/p/13193285.html,主要写用pydoc命令查看程序的说明性文档的方法。
原文:https://www.cnblogs.com/luoxun/p/13276617.html