首页 > 编程语言 >【笨方法学python】ex25 - 更多更多的练习 - 调用

【笨方法学python】ex25 - 更多更多的练习 - 调用

时间:2022-10-08 03:55:06浏览次数:89  
标签:word sentence python ex25 words print 方法学

尝试将自己写的方法作为模块进行导入。
ex25.py 代码如下:

点击查看代码
# coding=utf-8
# 更多更多的练习

def break_words(stuff):  # 定义 stuff 函数
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words


# Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分割成 (num+1) 个子字符串。


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)  # 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)
    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)
    print_first_word(words)
    print_last_word(words)


def filename_get():  # 获取文件名
    name = "ex25"
    return name

新建文件夹 "C:\Python27_myFunction"
将自己写的模块移至该目录下
新建系统环境变量 “PYTHONPATH” ,将新建目录设置为环境变量。
image

设置后即可导入模块。

新建 ex25 文件:

点击查看代码
A Blooming Tree
May Buddha let us meet
in my most beautiful hours,
I have prayed for it
for five hundred years
Buddha made me a tree
by the path you may take,
In full blossoms
Im waiting in the sun
every flower carrying my previous hope
As you are near, listen carefully
the quivering leaves are my waiting zeal,
As you pass by the tree
without noticing me,My friend, upon the ground behind you
is not the fallen petals but my withered heart

在 ex25 文件夹下打开命令行执行命令

break_words函数
点击查看代码
D:\tools\python学习\笨方法学python>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25
>>> target = open(ex25.filename_get())
>>> txt = target.read()
>>> break_words = ex25.break_words(txt)
>>> print break_words
['A', 'Blooming', 'Tree\nMay', 'Buddha', 'let', 'us', 'meet\nin', 'my', 'most', 'beautiful', 'hours,\nI', 'have', 'prayed', 'for', 'it\nfor', 'five', 'hundred', 'years\nBuddha', 'made', 'me', 'a', 'tree\nby', 'the', 'path', 'you', 'may', 'take,\nIn', 'full', 'blossoms\nIm', 'waiting', 'in', 'the', 'sun\nevery', 'flower', 'carrying', 'my', 'previous', 'hope\nAs', 'you', 'are', 'near,', 'listen', 'carefully\nthe', 'quivering', 'leaves', 'are', 'my', 'waiting', 'zeal,\nAs', 'you', 'pass', 'by', 'the', 'tree\nwithout', 'noticing', 'me,My', 'friend,', 'upon', 'the', 'ground', 'behind', 'you\nis', 'not', 'the', 'fallen', 'petals', 'but', 'my', 'withered', 'heart']
>>>

image

sort_words函数
点击查看代码
D:\tools\python学习\笨方法学python>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25
>>> target = open(ex25.filename_get())
>>> txt = target.read()
>>> sort_words = ex25.sort_words(txt)
>>> print sort_words
['\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ',', ',', ',', ',', ',', ',', 'A', 'A', 'A', 'B', 'B', 'B', 'I', 'I', 'I', 'M', 'M', 'T', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'k', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'q', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'v', 'v', 'v', 'v', 'v', 'v', 'w', 'w', 'w', 'w', 'w', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'z']
>>>
![image](/i/l/?n=22&i=blog/2279318/202210/2279318-20221008033511708-425286527.png)
点击查看代码
D:\tools\python学习\笨方法学python>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25
>>> List1 = ['First', 'Second', 'Third']
>>> print ex25.print_first_word(List1)
First
None
>>> print ex25.print_last_word(List1)
Third
None
>>> print List1
['Second']
>>>

image

sort_sentence 函数
点击查看代码
D:\tools\python学习\笨方法学python>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25
>>> target = open(ex25.filename_get())
>>> txt = target.read()
>>> sort_sentence = ex25.sort_sentence(txt)
>>> print sort_sentence
['A', 'Blooming', 'Buddha', 'Tree\nMay', 'a', 'are', 'are', 'beautiful', 'behind', 'blossoms\nIm', 'but', 'by', 'carefully\nthe', 'carrying', 'fallen', 'five', 'flower', 'for', 'friend,', 'full', 'ground', 'have', 'heart', 'hope\nAs', 'hours,\nI', 'hundred', 'in', 'it\nfor', 'leaves', 'let', 'listen', 'made', 'may', 'me', 'me,My', 'meet\nin', 'most', 'my', 'my', 'my', 'my', 'near,', 'not', 'noticing', 'pass', 'path', 'petals', 'prayed', 'previous', 'quivering', 'sun\nevery', 'take,\nIn', 'the', 'the', 'the', 'the', 'the', 'tree\nby', 'tree\nwithout', 'upon', 'us', 'waiting', 'waiting', 'withered', 'years\nBuddha', 'you', 'you', 'you', 'you\nis', 'zeal,\nAs']
>>>
![image](/i/l/?n=22&i=blog/2279318/202210/2279318-20221008034333877-766393052.png) ##### print_first_and_last & print_first_and_last)sorted函数
点击查看代码
D:\tools\python学习\笨方法学python>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25
>>> target = open(ex25.filename_get())
>>> txt = target.read()
>>> print ex25.print_first_and_last(txt)
A
heart
None
>>> print ex25.print_first_and_last_sorted(txt)
A
zeal,
As
None
>>>
![image](/i/l/?n=22&i=blog/2279318/202210/2279318-20221008034649122-1579165765.png)

标签:word,sentence,python,ex25,words,print,方法学
From: https://www.cnblogs.com/TiramisuPS/p/16767807.html

相关文章