【2017-09-07】數(shù)據(jù)結(jié)構(gòu)與算法(六)

序列

  • 命名切片
    問題:如果代碼中出現(xiàn)大量的硬編碼下標(biāo)值博秫,那么可讀性及可維護(hù)性都變得很低
    方案:
    使用內(nèi)置的slice()函數(shù)創(chuàng)建一個(gè)切片對(duì)象,切片對(duì)象可調(diào)用的屬性及方法
    • start
    • stop
    • step
    • indices(size)
      調(diào)用切片對(duì)象方法indices(size) 將它映射到一個(gè)確定大小的序列上潦牛,這個(gè)方法返回一個(gè)三元組 (start, stop, step)。所有值都會(huì)被合適的縮小以滿足邊界限制挡育,從而使用的時(shí)候避免出現(xiàn) IndexError 異常巴碗。
>>> items = [0, 1, 2, 3, 4, 5, 6]
>>>#一般寫法
>>> items[2:4]
[2, 3]
>>>#使用切片函數(shù)
>>> pice=slice(2,4)
>>> items[pice]
[2, 3]
>>> items[2:4]=[6,7]
>>> items
[0, 1, 6, 7, 4, 5, 6]
>>> items[pice]=[9,10]
>>> items
[0, 1, 9, 10, 4, 5, 6]
>>> pice.start
2
>>> pice.indices(len(items))
(2, 4, 1)
>>>#切片對(duì)象值stop最大為序列長度
>>> pice=slice(2,10)
>>> items[pice]
[9, 10, 4, 5, 6]
>>> pice.indices(len(items))
(2, 7, 1)
>>>#注意*的使用
>>> for i in range(*pice.indices(len(items))):
    print(items[i])
    
9
10
4
5
6
  • 統(tǒng)計(jì)序列中最大的元素
    • 手動(dòng)計(jì)算
    • 運(yùn)用collections模塊中的Counter 類
      手動(dòng)計(jì)算略過,可以通過字典等方式去實(shí)現(xiàn)即寒,本篇重點(diǎn)學(xué)習(xí)Counter類的使用橡淆。Counter 類實(shí)例對(duì)象有很多,常用的有:
      • most_common() 母赵,默認(rèn)或者參數(shù)大于一定的數(shù)量逸爵,返回整個(gè)結(jié)果
      • iitems()
      • elements
        等等
>>> from collections import Counter
>>> words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
>>> words_count=Counter(words)
>>> words_count
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, "don't": 1, "you're": 1, 'not': 1, 'under': 1})
>>> words_count['eyes']
8
>>> words_count.most_common(1)
[('eyes', 8)]

另外,Counter 實(shí)例一個(gè)鮮為人知的特性是它們可以很容易的跟數(shù)學(xué)運(yùn)算操作相結(jié)合凹嘲。

>>> from collections import Counter
>>> words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
>>> moreword=['why','are','you','not','looking','in','my','eyes']
>>> words_count=Counter(words)
>>> moreword_count=Counter(moreword)
>>> words_count
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, "don't": 1, "you're": 1, 'not': 1, 'under': 1})
>>> moreword_count
Counter({'in': 1, 'eyes': 1, 'looking': 1, 'why': 1, 'my': 1, 'not': 1, 'you': 1, 'are': 1})
>>> words_count+moreword_count
Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2, 'around': 2, 'looking': 1, 'under': 1, 'you': 1, 'in': 1, "don't": 1, 'are': 1, "you're": 1, 'why': 1})
>>> words_count-moreword_count
Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2, "don't": 1, 'under': 1, "you're": 1})
>>> 
  • 通過某個(gè)關(guān)鍵字排序一個(gè)字典列表
    問題:字典列表师倔,需要根據(jù)字典的一個(gè)或者多個(gè)字段排序
    方案:
    - 匿名函數(shù)實(shí)現(xiàn)
    - operator 模塊的 itemgetter 函數(shù)
    ?? 匿名函數(shù)lambda實(shí)現(xiàn)
>>> rows = [
{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]
>>> sorted(rows,key=lambda r:r["fname"])
[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}, {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'}, {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]
>>> sorted(rows,key=lambda r:(r["fname"],r["uid"]))
[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}, {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'}, {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]
>>> 

運(yùn)用itemgetter() 函數(shù),它負(fù)責(zé)創(chuàng)建一個(gè) callable 對(duì)象周蹭,傳遞多個(gè)參數(shù)時(shí)趋艘,函數(shù)返回一個(gè)元組,多條件排序的順序?qū)凑赵M的順序凶朗。
示例1

#上述示例可變?yōu)椋?>>> from operator import itemgetter
>>> rows_by_fname = sorted(rows, key=itemgetter('fname'))
>>> rows_by_fname_and_uid=sorted(rows, key=itemgetter('fname',"uid"))
>>> rows_by_fname
[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}, {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'}, {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]
>>> rows_by_fname_and_uid
[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}, {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'}, {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]
>>> #key接收一個(gè)函數(shù)瓷胧,指明按照什么排序
>>> min(rows, key=itemgetter('uid'))
{'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}
>>> 

示例2

>>> from operator import itemgetter

>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

>>> sorted(students, key = itemgetter(2))

[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>> 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市棚愤,隨后出現(xiàn)的幾起案子搓萧,更是在濱河造成了極大的恐慌,老刑警劉巖遇八,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件矛绘,死亡現(xiàn)場離奇詭異,居然都是意外死亡刃永,警方通過查閱死者的電腦和手機(jī)货矮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來斯够,“玉大人囚玫,你說我怎么就攤上這事《凉妫” “怎么了抓督?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長束亏。 經(jīng)常有香客問我铃在,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任定铜,我火速辦了婚禮阳液,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘揣炕。我一直安慰自己帘皿,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布畸陡。 她就那樣靜靜地躺著鹰溜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪丁恭。 梳的紋絲不亂的頭發(fā)上曹动,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天,我揣著相機(jī)與錄音牲览,去河邊找鬼仁期。 笑死,一個(gè)胖子當(dāng)著我的面吹牛竭恬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播熬的,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼痊硕,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了押框?” 一聲冷哼從身側(cè)響起岔绸,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎橡伞,沒想到半個(gè)月后盒揉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡兑徘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年刚盈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片挂脑。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡藕漱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出崭闲,到底是詐尸還是另有隱情肋联,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布刁俭,位于F島的核電站橄仍,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜侮繁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一虑粥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鼎天,春花似錦舀奶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至罗岖,卻和暖如春涧至,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背桑包。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國打工南蓬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人哑了。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓赘方,卻偏偏與公主長得像,于是被迫代替她去往敵國和親弱左。 傳聞我的和親對(duì)象是個(gè)殘疾皇子窄陡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

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