本次所使用到的函数
- split(‘a’,b) 分割作用,a为分隔符,b为分割后取到第几个元素 split详解
- sort() 排序,默认是升序,如果要降序,可以使用sort(reverse=True) sort详解
- pop()函数用于移除列表中的最后一个元素(默认情况),也可以指定pop(#),#为元素的位置 pop详解
脚本情况
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ') #split()为分割函数,后面的''表示分隔符,此处是空格,也即将每个字符串根据空格分割开来
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words) #sort()函数可以对相应参数进行排序,默认是升序(a-z)
def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0) #pop(0)将第一个单词从words中移除,并且返回到word中,所以此时word即为第一个单词,注意,顺序从左往右为0开始;从右往左为-1开始
print(word)
def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1) #pop(-1)将最后一个单词从works中移除,并且返回到word中,所以此时word即为最后一个单词
print(word)
def sort_sentence(sentence):
'''Takes in a full sentence and returns the sorted words.'''
words = break_words(sentence) #调用break_words函数将sentence分割成一个个单词
return sort_words(words) #返回值调用sort_words函数将分割的单词进行升序排序
def print_first_and_last_sentence(sentence):
'''Prints the first and last words of the sentence.'''
words = break_words(sentence) #分割sentence为一个个单词
print_first_word(words) #输出第一个单词
print_last_word(words) #输出最后一个单词
def print_first_and_last_sorted(sentence):
'''Sorts the words then prints the first and last one.'''
words = sort_sentence(sentence) #对sentence进行升序排序,返回words
print_first_word(words) #输出第一个单词
print_last_word(words) #输出最后一个单词
运行情况,不是直接运行,是先import导入这个模块(不用带py这个后缀),再使用ex25里面的函数
PS E:\tonyc\Documents\Vs workspace\The Hard Way> python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'ex25.py'; 'ex25' is not a package
>>> 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 可以看到,此时words的原首尾单词没有了,这是pop的特性,选中的单词会被去除
['good', 'things', 'come', 'to', 'those', 'who']
>>> sentence
'All good things come to those who wait.'
>>> ex25.print_first_and_last_sentence(sentence)
All
wait.
>>> sentence
'All good things come to those who wait.'
>>> ex25.print_first_and_last_sorted(sentence)
All
who
>>>
注意
- 试着执行 help(ex25) 和 help(ex25.break_words) 。这是你得到模组帮助文档的方式。 所谓帮助文档就是你定义函数时放在 “”" 之间的东西,它们也被称作documentation comments (文档注解),后面你还会看到更多类似的东西。
>>> help(ex25) 默认只输出第一个函数的文档
Help on module ex25:
NAME
ex25
FUNCTIONS
break_words(stuff)
This function will break up words for us.
>>> help(ex25.sort_words)
Help on function sort_words in module ex25:
sort_words(words)
Sorts the words.
- 重复键入 ex25. 是很烦的一件事情。有一个捷径就是用 from ex25 import * 的方式导入模组。这相当于说: “我要把 ex25 中所有的东西 import 进来。 ”程序员喜欢说这样的倒装句,开一个新的会话,看看你所有的函数是不是已经在那里了。
3.对于split,如果不添加任何参数,则默认是以空格为分隔符,也即
split() 相当于 split(’ ‘)有空格,如果split(’’)没有空格则会报错