《笨辦法學(xué)Python》筆記19-----更多更多的練習(xí)

這節(jié)復(fù)習(xí)函數(shù)的定義和調(diào)用嵌器,以及模塊導(dǎo)入

詳細(xì)內(nèi)容見笨辦法筆記17

教材代碼

#ex25.py

def break_words(stuff):

??? words = stuff.split(',')

??? return words

def sort_words(words):

??? return sorted(words)

def print_first_word(words):

??? word = words.pop(0)

??? return word

def print_last_word(words):

??? word = words.pop(-1)

??? print word

def sort_sentence(sentence):

??? words = break_words(sentence)

??? return sort_words(words)

def print_first_and_last(sentence):

??? words = break_words(sentence)

??? print_first_word(words)

??? print_last_word(words)

def print_first_and_last_sorted(sentence):

??? words = sort_sentence(sentence)

??? print_first_word(words)

??? print_last_word(words)

1.函數(shù)的定義

以關(guān)鍵字def開始

函數(shù)名能體現(xiàn)出函數(shù)的功能

括號(hào)中可使用參數(shù)肛真,括號(hào)后面緊跟冒號(hào)

函數(shù)體以相同縮進(jìn)為標(biāo)志


2.函數(shù)的調(diào)用

函數(shù)需先定義才可對(duì)其進(jìn)行調(diào)用

3.模塊的導(dǎo)入

將ex25.py導(dǎo)入到另一個(gè)腳本中,以便能使用ex25.py中定義的函數(shù)爽航。

import ex25

或者

from ex25 import *

4.代碼中涉及到幾個(gè)新的函數(shù)

split

str.split([sep[, maxsplit]])

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).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

For example, ' 1? 2? 3? '.split() returns ['1', '2', '3'], and '? 1? 2? 3? '.split(None, 1) returns ['1', '2? 3? '].

split()函數(shù)根據(jù)分隔符對(duì)字符串進(jìn)行分隔成段蚓让。返回一個(gè)列表。

參數(shù)中可設(shè)置分隔符和最大分隔次數(shù)


sorted

sorted(iterable[, cmp[, key[, reverse]]])

Return a new sorted list from the items in iterable.

The optional arguments cmp, key, and reverse have the same meaning as those for the list.sort() method (described in section Mutable Sequence Types).

cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.

key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once. Use functools.cmp_to_key() to convert an old-style cmp function to a key function.

The built-in sorted() function is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).

sorted()對(duì)對(duì)象進(jìn)行排序讥珍,最后一個(gè)參數(shù)可用于設(shè)置升序或降序排列


pop

? pop(...)

???? L.pop([index]) -> item -- remove and return item at index (default last).

???? Raises IndexError if list is empty or index is out of range.

pop() 函數(shù)用于移除列表中的一個(gè)元素(默認(rèn)最后一個(gè)元素)历极,并且返回該元素的值。

如果列表為空或者索引超出范圍衷佃,會(huì)引發(fā)一個(gè)IndexError異常趟卸。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市氏义,隨后出現(xiàn)的幾起案子锄列,更是在濱河造成了極大的恐慌,老刑警劉巖惯悠,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件邻邮,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡克婶,警方通過查閱死者的電腦和手機(jī)筒严,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)情萤,“玉大人鸭蛙,你說(shuō)我怎么就攤上這事〗畹海” “怎么了规惰?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)泉蝌。 經(jīng)常有香客問我,道長(zhǎng)揩晴,這世上最難降的妖魔是什么勋陪? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮硫兰,結(jié)果婚禮上诅愚,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好违孝,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布刹前。 她就那樣靜靜地躺著,像睡著了一般雌桑。 火紅的嫁衣襯著肌膚如雪喇喉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天校坑,我揣著相機(jī)與錄音拣技,去河邊找鬼。 笑死耍目,一個(gè)胖子當(dāng)著我的面吹牛膏斤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播邪驮,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼莫辨,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了毅访?” 一聲冷哼從身側(cè)響起沮榜,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎俺抽,沒想到半個(gè)月后敞映,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡磷斧,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年振愿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弛饭。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡冕末,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出侣颂,到底是詐尸還是另有隱情档桃,我是刑警寧澤,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布憔晒,位于F島的核電站藻肄,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏拒担。R本人自食惡果不足惜嘹屯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望从撼。 院中可真熱鬧州弟,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至啃奴,卻和暖如春潭陪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背纺腊。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工畔咧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人揖膜。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓誓沸,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親壹粟。 傳聞我的和親對(duì)象是個(gè)殘疾皇子拜隧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容