她是你的谁 发表于 2021-9-2 13:03:58

python 超好用的迭代兵器库itertools,十八般兵器哪18般?

知识点

在古典小说和传统评话中,常说武艺高强的人是“十八般武艺样样醒目”,这十八般武艺是指使用“十八般兵器”的功夫和技能。哪十八般呢?
十八般兵器在武术界中最普遍的说法是:刀、枪、剑、戟、斧、钺、钩、叉、鞭、锏、锤、抓、镗、棍、槊、棒、拐、流星。

汉武于元封四年(公元前107),经过严格的挑选和整理,筛选出18种范例的兵器:矛、镗、刀、戈、槊、鞭、锏、剑、锤、抓、戟、弓、钺、斧、牌、棍、枪、叉。
三国期间,著名的兵器鉴别家吕虔,根据兵器的特点,对汉武帝钦定的“十八般兵器”重新排列为九长九短。九长:戈、矛、戟、槊、镗、钺、棍、枪、叉;九短:斧、戈、牌、箭、鞭、剑、锏、锤、抓。
明代《五杂俎》和清代《坚集》两书所载,“十八般兵器”为弓、弩、枪、刀、剑、矛、盾、斧、钺、戟、黄、锏、挝、殳(棍)、叉、耙头、锦绳套索、白打(拳术)。后人称其为“小十八般”。
迭代器

也叫生成器,它最大的优势就是延迟计算按需使用,节省内存空间、提高运行效率。
迭代工具库 itertools 中共有18个函数,恰好似“迭代界”的十八般兵器,掌握了这些功夫和技能也可以说是“十八般武艺样样醒目”!:
>>> import itertools
>>> tools = >='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)))

>>> 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),,{6,7},(i for i in range(8,11)))
<itertools.chain object at 0x0A0BF3B8>
>>> list(it.chain(range(4),,{6,7},(i for i in range(8,11))))

>>> 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. 排列器 permutations

>>> import itertools as it
>>> list(it.permutations())
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
>>> # 数字1、2、3能组成哪些三位数?
>>> *100+i*10+i for i in it.permutations()]

>>> 6. 压缩器 compress

按照真值表来精简迭代器,筛选出部分值
>>> import itertools as it
>>> i = it.compress(range(6), (1,1,0,0,1,0))
>>> list(i)

>>> 7. 切片器 islice

>>> import itertools as it
>>> islice = it.islice(range(100),0,9,2)
>>> list(islice)

>>> iSlice = it.islice(range(1,100),0,9,2)
>>> list(iSlice)

>>> # 可以不指定起始和步长,直接指定个数
>>> list(it.islice(range(1,100),10))

>>> 8. 计数器 count

因为生成器只提供说法不是数据集,直接用 list(count1)会死循环的,可以用islice()指定一下个数。
>>> import itertools as it
>>> count1 = it.count(start=0,step=3)
>>> list(it.islice(count1,12))

>>> count2 = it.count(start=100,step=-2)
>>> list(it.islice(count2,10))

>>> 9. 循环器 cycle

>>> import itertools as it
>>> list(it.islice(it.cycle(&#39;ABC&#39;),10))
[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;A&#39;]
>>> list(it.islice(it.cycle(),10))

>>> 10. 重复器 repeat

>>> import itertools as it
>>> list(it.repeat(5,10))

>>> list(it.repeat(,5))
[, , , , ]
>>> 11. 舍真器 dropwhile

舍弃不满足条件的元素,但当条件不满足即制止筛选
>>> import itertools as it
>>> lst =
>>> list(it.dropwhile(lambda i:i<9,lst))

>>> list(it.dropwhile(lambda i:i%2,lst))

>>> 12. 留真器 takewhile

留下满足条件的元素,但当条件不满足即制止筛选
>>> import itertools as it
>>> list(it.takewhile(lambda i:i<6, range(10)))

>>> lst =
>>> list(it.takewhile(lambda i:i<11,lst))

>>> list(it.takewhile(lambda i:i%6,lst))

>>> 13. 筛假器 filterfalse

舍弃满足条件的所有元素,留下所有不满足条件的
>>> import itertools as it
>>> lst =
>>> list(it.filterfalse(lambda i:i<9,lst))

>>> list(it.filterfalse(lambda i:i%2,lst))

>>> 14. 分组器 groupby

>>> import itertools as it
>>> group = it.groupby(range(20), lambda i:not 8<i<16)
>>> for i,j in group: print(i,list(j))

True
False
True
>>> 15. 乘积器 product

>>> import itertools as it
>>> list(it.product(&#39;ABC&#39;,(1,2)))
[(&#39;A&#39;, 1), (&#39;A&#39;, 2), (&#39;B&#39;, 1), (&#39;B&#39;, 2), (&#39;C&#39;, 1), (&#39;C&#39;, 2)] 16. 映射器 starmap

>>> import itertools as it
>>> list(it.starmap(str.isupper, &#39;AbCDefgH&#39;))

>>> list(it.starmap(lambda a,b,c:a+b+c,(,,)))

>>> list(it.starmap(lambda *a:sum(a),))

>>> 17. 元组器 tee

返回多个迭代器的元组
>>> import itertools as it
>>> ,3)]
[, , ]
>>> it.tee(,3)
(<itertools._tee object at 0x030711A8>,
<itertools._tee object at 0x03078228>,
<itertools._tee object at 0x0131FFE8>)
>>> 18. 打包器 zip_longest

与内置函数zip()类似,但元素个数以最长的迭代器为准
>>> import itertools as it
>>> list(it.zip_longest(&#39;ABCDE&#39;,range(1,4)))
[(&#39;A&#39;, 1), (&#39;B&#39;, 2), (&#39;C&#39;, 3), (&#39;D&#39;, None), (&#39;E&#39;, None)]
>>> list(zip(&#39;ABCDE&#39;,range(1,4)))
[(&#39;A&#39;, 1), (&#39;B&#39;, 2), (&#39;C&#39;, 3)]
>>> list(it.zip_longest(&#39;ABCDE&#39;,range(1,4),))
[(&#39;A&#39;, 1, 1), (&#39;B&#39;, 2, 2), (&#39;C&#39;, 3, 3), (&#39;D&#39;, None, 4), (&#39;E&#39;, None, None)]
>>> 名字我随便起的,形像就好。看下来怎样?十八兵器,样样醒目了吗?其实掌握个几样“称手的”即可,何必面面俱到呢 ^_^

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: python 超好用的迭代兵器库itertools,十八般兵器哪18般?