《笨办法学 python 3》25.更多更多的练习

知识点:

1. split()       :分割字符串,可以在()内指定分隔符,分隔符把字符串分割成若干部分,放在list中,需要注意的是分隔符会消失,被舍弃掉;

split('z',2)[1] :意思是以z为分隔符,分割两次(后面还有z的话也不再进行分割),[1]的意思是取第一个值;2和[1]不是必填;

2. sorted()   :它是python的内置函数,作用是对list进行排序,它和成员函数list.sort()不同的是,前者是新建一个新的list然后进行排序,而list.sort()则是对已有的list本身进行操作;

sorted():它默认是以顺序排列的,其语法比较复杂: sorted(iterable, / , * ,key=None,reverse=False)

iterable:可迭代对象(比如下文中的list)  

/   *   啥意思还不明白,后面补充...

key : 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序,可不填,默认none;

reverse :排序规则,=False(默认)时是升序排列,=True时是降序排列

3. pop()       :。。。

基础练习:

def break_words(stuff):
	"""This function will break up words for us.///这个函数会为我们分解单词。"""
	words = stuff.split(' ') '''分割句子'''
	return words

def sort_words(words):
	"""Sorts the words."""
	return sorted(words) '''返回排序结果'''

def print_first_word(words):
	"""Prints the first word after popping it off.///把第一个单词打印出来后打印出来。"""
	word = words.pop(0) '''提取第一个字符'''
	print(word)  '''打印'''

def print_last_word(words):
	"""Prints the last word after popping it off.///把最后一个单词打印出来。"""
	word = words.pop(-1) '''提取最后一个字符'''
	print(word) '''打印'''

def sort_sentence(sentence):
	"""Takes in a full sentence and returns the sorted words.///取一个完整的句子,并返回已排序的单词。"""
	words = break_words(sentence) '''调用函数,分割句子'''
	return sort_words(words) '''返回排序结果'''

def print_first_and_last(sentence):
	"""Prints the first and last words of the sentence.///打印句子的第一个和最后一个单词。"""
	words = break_words(sentence) '''调用函数,分割句子'''
	print_first_word(words) '''调用函数,提取第1个字符并打印'''
	print_last_word(words) '''调用函数,提取最后1个字符并打印'''

def print_first_and_last_sorted(sentence):
	"""Sorts the words then prints the first and last one.///把单词分类然后打印第一个和最后一个。"""
	words = sort_sentence(sentence) '''调用函数,分割句子,重新排序'''
	print_first_word(words) '''调用函数,提取第1个字符并打印'''
	print_last_word(words) '''调用函数,提取最后1个字符并打印'''

在python内运行ex25:

注意:执行代码的方式,打开python解释器,进入内部,“>>>”的标记代表进入了内部 

下面是python导入ex25后需要键入的代码

'''
下面这几段是在python内输入的,码好了直接一行一行的复制进去:
------------------------------------------------------
import ex25
sentence = "All good things come to those who wait."
words = ex25.break_words(sentence)
words
sorted_words = ex25.sort_words(words)
sorted_words
ex25.print_first_word(words)
ex25.print_last_word(words)
words
ex25.print_first_word(sorted_words)
ex25.print_last_word(sorted_words)
sorted_words
sorted_words = ex25.sort_sentence(sentence)
sorted_words
ex25.print_first_and_last(sentence)
ex25.print_first_and_last_sorted(sentence)
------------------------------------------------------
'''

结果:

结果分析:

#打开python解释器,进入python内部
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

#把整个ex25导入python,两种写法: from ex25 import *  从ex25导入所有东西
>>> import ex25 
#变量,要记住
>>> sentence = "All good things come to those who wait."
#调用ex25的函数 break_words,它的作用是切割句子(默认以空格为分割,分割点会消失),把切割后的集合赋值给 words
>>> words = ex25.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.'] #分割后的字符集

#调用ex25的函数 sort_words, 它的作用是对words进行排序,然后重新赋值给 sorted_words
>>> sorted_words = ex25.sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who'] #重新排序的字符集

#调用ex25的函数 print_first_word,它的作用是获取words的第一个字符,并打印出来
>>> ex25.print_first_word(words)
All
#调用ex25的函数 print_last_word,它的作用是获取words的最后一个字符,并打印出来
>>> ex25.print_last_word(words)
wait.
#words被获取第一、最后一个字符后
>>> words
['good', 'things', 'come', 'to', 'those', 'who']

#调用ex25的函数 print_first_word,它的作用是获取sorted_words的第一个字符,并打印出来
>>> ex25.print_first_word(sorted_words)
All
#调用ex25的函数 print_last_word,它的作用是获取sorted_words的最后一个字符,并打印出来
>>> ex25.print_last_word(sorted_words)
who
#sorted_words被提取第一、最后一个字符后
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']

#调用ex25的函数 sorted_words, 分割句子、进行排序,并赋值给sorted_words
>>> sorted_words = ex25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']

#调用ex25的函数 print_first_and_last, 分割句子、提取并打印第一、最后一个字符
>>> ex25.print_first_and_last(sentence)
All
wait.

#调用ex25的函数 print_first_and_last_sorted, 分割句子、重新排序、提取并打印第一、最后一个字符
>>> ex25.print_first_and_last_sorted(sentence)
All
who
>>>

没有难点,要的是耐心还有记住每个函数和函数的作用!!!

END!!!

猜你喜欢

转载自blog.csdn.net/waitan2018/article/details/82749108