练习25--更多更多练习

一 相关知识

1 split()方法

  • 描述:Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
  • 语法:
    str.split(str="", num=string.count(str))
  • 参数说明
    • str -- 分隔符,默认为所有的空字符,包括空格、换行( )、制表符( )等。
    • num -- 分割次数。默认为 -1, 即分隔所有。
  • 返回值:返回分割后的字符串列表。

2 sorted()函数

  • 描述:sorted() 函数对所有可迭代的对象进行排序操作。
  • 语法:

    sorted(iterable, cmp=None, key=None, reverse=False)
  • 参数说明
    • iterable -- 可迭代对象。
    • cmp -- 比较的函数,它具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0。
    • key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
    • reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
  • 返回值:返回重新排序的列表。
  • 和sort()函数的区别:
    • sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
    • list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。

3 pop()函数

(1)列表:

  • 描述:用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
  • 语法:
    list.pop(obj=list[-1])
  • 参数说明:obj – 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。
  • 返回值:该方法返回从列表中移除的元素对象。

(2)字典

  • 描述:Python 字典 pop() 方法删除字典给定键 key 及对应的值,返回值为被删除的值。key 值必须给出。 否则,返回 default 值。
  • 语法:
    pop(key[,default])
  • 参数说明:
    • key: 要删除的键值
    • default: 如果没有 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_work4_python2_code_python2_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_work4_python2_code_python2_learnpythonthehardwayex25.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