知識點
在古典小說和傳統評話中,常說武藝高強的人是“十八般武藝樣樣精通”,這十八般武藝是指使用“十八般兵器”的功夫和技能,哪十八般呢?
十八般兵器在武術界中最普遍的說法是:刀、槍、劍、戟、斧、鉞、鉤、叉、鏜、棍、槊、棒、鞭、锏、錘、抓、拐子、流星,
漢武于元封四年(公元前107),經過嚴格的挑選和整理,篩選出18種型別的兵器:矛、鏜、刀、戈、槊、鞭、锏、劍、錘、抓、戟、弓、鉞、斧、牌,棍、槍、叉,
三國時代,著名的兵器鑒別家呂虔,根據兵器的特點,對漢武帝欽定的“十八般兵器”重新排列為九長九短,九長:戈、矛、戟、槊、鏜、鉞、棍、槍、叉;九短:斧、戈、牌、箭、鞭、劍、锏、錘、抓,
明代《五雜俎》和清代《堅集》兩書所載,“十八般兵器”為弓、弩、槍、刀、劍、矛、盾、斧、鉞、戟、黃、锏、撾、殳(棍)、叉、耙頭、錦繩套索、白打(拳術),后人稱其為“小十八般”,
迭代器,也叫生成器,它最大的優勢就是延遲計算按需使用,節省記憶體空間、提高運行效率,
迭代工具庫 itertools 中共有18個函式,恰好似“迭代界”的十八般兵器,掌握了這些功夫和技能也可以說是“十八般武藝樣樣精通”!:
>>> import itertools
>>> tools = [func for func in dir(itertools) if func[0]>='a']
>>> len(tools)
18
>>> tools
['accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress',
'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'permutations',
'product', 'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']
1. 累加器 accumulate
>>> import itertools as it
>>> it.accumulate(range(11))
<itertools.accumulate object at 0x0A0C9988>
>>> list(it.accumulate(range(11)))
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
>>>
1乘2乘3...一直乘到n有階乘運算 n! ,但1加2加3...一直加到n,一般都沒有定義“累和”運算,還有用個回圈來計算,現在有了這個函式可以代替用用的,比如1加到100:
>>> list(it.accumulate(range(1+100)))[-1]
5050
>>>
2. 物件聯結器 chain
連接多個迭代器,或其它可迭代物件
>>> import itertools as it
>>> it.chain(range(3),[3,4,5],{6,7},(i for i in range(8,11)))
<itertools.chain object at 0x0A0BF3B8>
>>> list(it.chain(range(4),[4,5],{6,7},(i for i in range(8,11))))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
3. 組合器 combinations
from itertools import combinations as comb
>>> comb1 = comb(range(4), 3)
>>> list(comb1)
[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
>>> comb2 = comb(range(1,6), 3)
>>> list(comb2)
[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5),
(1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)]
>>> comb3 = comb(range(1,6), 4)
>>> list(comb3)
[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]
>>>
4. 可重復組合器 combinations_with_replacement
>>> from itertools import combinations_with_replacement as Comb2
>>> comb1 = Comb2(range(4), 3)
>>> list(comb1)
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 1, 1), (0, 1, 2), (0, 1, 3),
(0, 2, 2), (0, 2, 3), (0, 3, 3), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 2),
(1, 2, 3), (1, 3, 3), (2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)]
>>> comb2 = Comb2(range(1,6), 3)
>>> list(comb2)
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 2, 2), (1, 2, 3),
(1, 2, 4), (1, 2, 5), (1, 3, 3), (1, 3, 4), (1, 3, 5), (1, 4, 4), (1, 4, 5),
(1, 5, 5), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 2, 5), (2, 3, 3), (2, 3, 4),
(2, 3, 5), (2, 4, 4), (2, 4, 5), (2, 5, 5), (3, 3, 3), (3, 3, 4), (3, 3, 5),
(3, 4, 4), (3, 4, 5), (3, 5, 5), (4, 4, 4), (4, 4, 5), (4, 5, 5), (5, 5, 5)]
>>>
5. 精簡壓縮器 compress
按照真值表來精簡迭代器,篩選出部分值
>>> import itertools as it
>>> i = it.compress(range(6), (1,1,0,0,1,0))
>>> list(i)
[0, 1, 4]
>>>
6. 切片器 islice
>>> import itertools as it
>>> islice = it.islice(range(100),0,9,2)
>>> list(islice)
[0, 2, 4, 6, 8]
>>> iSlice = it.islice(range(1,100),0,9,2)
>>> list(iSlice)
[1, 3, 5, 7, 9]
>>> # 可以不指定起始和步長,直接指定個數
>>> list(it.islice(range(1,100),10))
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
>>>
7. 計數器 count
因為生成器只提供說法不是資料集,直接用 list(count1)會死回圈的,可以用islice()指定一下個數,
>>> import itertools as it
>>> count1 = it.count(start=0,step=3)
>>> list(it.islice(count1,12))
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33]
>>> count2 = it.count(start=100,step=-2)
>>> list(it.islice(count2,10))
[100, 98, 96, 94, 92, 90, 88, 86, 84, 82]
>>>
8. 回圈器 cycle
>>> import itertools as it
>>> list(it.islice(it.cycle('ABC'),10))
['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']
>>> list(it.islice(it.cycle([1,2,3,4]),10))
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
>>>
編輯中,,,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296956.html
標籤:其他
上一篇:python 股票盯盤v2.0
下一篇:2021-08-31
