序列
-
命名切片
問題:如果代碼中出現(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)]
>>>