Python之function interacting

例子

以一个字符串处理的例子来展示一些函数之间的调用,顺便将里面涉及到的Python3里面很强大的字符串处理功能解释解释。
首先新建一个文件,命名为untitled0.py,因为我懒得改名字了就用这个自动的吧。然后这个文件中放各种处理功能的函数。

#####function interact#####
###拆分句子函数###
def break_sentence(sentence):
    words = sentence.split(' ') #split函数,下面会介绍
    return words

###排序###
def sort_words(words):
    return sorted(words)

###打印第一个单词###
def print_first_word(words):
    first_word = words.pop(0)
    print("the first word is ",first_word)

###打印最后一个单词### 
def print_last_word(words):
    last_word = words.pop(-1)
    print("the last word is ",last_word)

###对句子的单词进行排序###    
def sort_sentence(sentence):
    words = break_sentence(sentence)
    return sort_words(words)

###打印句子的第一个和最后一个单词###
def print_first_last_word(sentence):
    words = break_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

###打印经过排序的第一个单词和最后一个单词###    
def print_last_and_first_sort(sentence):
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

接着新建一个文件,导入untitled0.py这个文件并调用其中的函数:

###导入untitled0.py文件
import untitled0

sentence = "Great minds think alike" #英雄所见略同

words = untitled0.break_sentence(sentence) #拆分句子
print("words are ",words)

sorted_words = untitled0.sort_words(words) #对单词进行排序
print("sorted words are ", sorted_words)

print("----------------------------------------")
untitled0.print_first_word(words) #打印第一个单词
untitled0.print_last_word(words) #打印最后一个单词

print("----------------------------------------")
untitled0.print_first_word(sorted_words) #打印排序后的第一个单词
untitled0.print_last_word(sorted_words) #打印排序后的最后一个单词

sorted_sentence = untitled0.sort_sentence(sentence) #对句子中的单词直接排序
print("sorted sentence is ",sorted_sentence)

print("----------------------------------------")
untitled0.print_first_last_word(sentence) #打印第一个和最后一个单词
untitled0.print_last_and_first_sort(sentence) #打印排序后的第一个和最后一个单词

运行后的结果如下:
这里写图片描述
这样就很清楚了。首先写了一个拆分句子的函数,并将拆分后的单词返回。写了一个排序的函数,并将排序后的单词组返回。利用这些处理过的单词进行接下来的步骤,如打印第一个单词,打印第二个单词等。

split()

Python3具有强大的字符串处理功能,其中文档中关于split()函数的介绍如下:

str.split(sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

里面有两个参数:分隔标准和分隔元素个数。如果给定了一个参数 maxsplit,且这个参数不是负数,分裂器至多会分裂出 maxsplit + 1 个元素。如果未给值,或者给了 -1作为值,那么就表示没有限制分裂数量,会尽可能的分裂。不多说,看例子来理解吧。

>>> '1,2,3'.split(',') #以逗号为分隔标准,不限分隔元素数量
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1) #以逗号为分隔标准,分隔两个元素
['1', '2,3']
>>> '1,2,,3,'.split(',') #以逗号为分隔标准
['1', '2', '', '3', '']

在上述例子中,分隔标准是’ ‘(空格符),将句子拆分成单词。

sorted()

sorted()这个函数是用来排序的,这个十分容易理解。例如下面的例子:

>>>a = [1,8,3,0,4]
>>>b = sorted(a)
>>>b
[0, 1, 3, 4, 8]

这是对数字的排序,那么对字符又是如何排序的呢,可以通过下面这个小例子进行验证:

>>>a = ['a','A','b','B','g','G','e','E','D','C','c','D','F','f']
>>>b=sorted(a)
>>>b
['A', 'B', 'C', 'D', 'D', 'E', 'F', 'G', 'a', 'b', 'c', 'e', 'f', 'g']

从上述例子可以看出,对于字符的排序,是先大写按照26字母顺序排然后才是小写按照26字母排序。

>>> student_tuples = [
        ('john', 'A', 15),
        ('jane', 'B', 12),
        ('dave', 'B', 10),
    ]
>>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

这个例子中加入了排序的依据,由key来定义。key为函数,指定取待排序元素的哪一项进行排序。在这里,key指定的是用第三个元素进行排序。

pop()

在官方文档中是这么写的:

list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

pop()为移除某一个元素并返回该元素的值。例如:

>>>words = ['love','me','love','my','dog']
>>>a = words.pop(0)
>>>a
'love'
>>>words
['me', 'love', 'my', 'dog']
>>> b = words.pop(-1)
>>>b
'dog'
>>>words
['me', 'love', 'my']

通过这个例子,提炼的知识点大概就这些了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值