当前位置:   article > 正文

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

知识点

在古典小说和传统评话中,常说武艺高强的人是“十八般武艺样样精通”,这十八般武艺是指使用“十八般兵器”的功夫和技能。哪十八般呢?

十八般兵器在武术界中最普遍的说法是:刀、枪、剑、戟、斧、钺、钩、叉、鞭、锏、锤、抓、镗、棍、槊、棒、拐、流星。

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

迭代器

迭代器的最大优势就是延迟计算按需使用,节省内存空间、提高运行效率。

迭代工具库 itertools 中共有18个函数,恰好似“迭代界”的十八般兵器,掌握了这些功夫和技能也可以说是“十八般武艺样样精通”!:

  1. >>> import itertools
  2. >>> tools = [func for func in dir(itertools) if func[0]>='a']
  3. >>> len(tools)
  4. 18
  5. >>> tools
  6. ['accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress',
  7. 'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'permutations',
  8. 'product', 'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']

1. 累加器 accumulate 

  1. >>> import itertools as it
  2. >>> it.accumulate(range(1,11))
  3. <itertools.accumulate object at 0x0A0C9988>
  4. >>> list(it.accumulate(range(1,11)))
  5. [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
  6. >>> list(it.accumulate([1]*10))
  7. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  8. >>>
  9. >>> L = [1]*10
  10. >>> list(it.accumulate(L))
  11. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  12. >>> L = list(it.accumulate(L))
  13. >>> list(it.accumulate(L))
  14. [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
  15. >>>

1乘2乘3...一直乘到n有阶乘运算 n! ,但1加2加3...一直加到n,一般都没有定义“累和”运算,还需循环来计算。现在有了这个函数可以代替用用的,比如1加到100:

  1. >>> list(it.accumulate(range(1+100)))[-1]
  2. 5050
  3. >>>

不用此库函数的代码实现:

  1. >>> a = [1 for _ in range(10)]
  2. >>> a
  3. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  4. >>> for i,n in enumerate(a):
  5. if i:a[i] += a[i-1] # 或 if i+1<len(a):a[i+1] += n
  6. >>> a
  7. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  8. >>> for i in range(1,len(a)):
  9. a[i] += a[i-1] # 这样原始的比较简洁
  10. >>> a
  11. [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
  12. >>>

还有另外两个参数 func 和  initial,可以完成其它自定义运算:

  1. >>> list(it.accumulate([1,2,3,4],lambda x,y:x+y))
  2. [1, 3, 6, 10]
  3. >>> list(it.accumulate([1,2,3,4],lambda x,y:x*y))
  4. [1, 2, 6, 24]
  5. >>> list(it.accumulate([1,2,3,4],lambda x,y:x-y))
  6. [1, -1, -4, -8]
  7. >>> list(it.accumulate([1,2,3,4],lambda x,y:y-x))
  8. [1, 1, 2, 2]
  9. >>> import operator
  10. >>> list(it.accumulate([1,2,3,4],operator.add))
  11. [1, 3, 6, 10]
  12. >>> list(it.accumulate([1,2,3,4],operator.sub))
  13. [1, -1, -4, -8]
  14. >>> list(it.accumulate([1,2,3,4],operator.mul))
  15. [1, 2, 6, 24]
  16. >>> list(it.accumulate([1,2,3,4], operator.mul, initial=2))
  17. [2, 2, 4, 12, 48]
  18. >>>

2. 连接器 chain

连接多个迭代器,或其它可迭代对象

  1. >>> import itertools as it
  2. >>> it.chain(range(3),[3,4,5],{6,7},(i for i in range(8,11)))
  3. <itertools.chain object at 0x0A0BF3B8>
  4. >>> list(it.chain(range(4),[4,5],{6,7},(i for i in range(8,11))))
  5. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  6. >>>

3. 组合器 combinations

  1. from itertools import combinations as comb
  2. >>> comb1 = comb(range(4), 3)
  3. >>> list(comb1)
  4. [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
  5. >>> comb2 = comb(range(1,6), 3)
  6. >>> list(comb2)
  7. [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5),
  8. (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)]
  9. >>> comb3 = comb(range(1,6), 4)
  10. >>> list(comb3)
  11. [(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]
  12. >>>

4. 可重复组合器 combinations_with_replacement

  1. >>> from itertools import combinations_with_replacement as Comb2
  2. >>> comb1 = Comb2(range(4), 3)
  3. >>> list(comb1)
  4. [(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 1, 1), (0, 1, 2), (0, 1, 3),
  5. (0, 2, 2), (0, 2, 3), (0, 3, 3), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 2),
  6. (1, 2, 3), (1, 3, 3), (2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)]
  7. >>> comb2 = Comb2(range(1,6), 3)
  8. >>> list(comb2)
  9. [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 2, 2), (1, 2, 3),
  10. (1, 2, 4), (1, 2, 5), (1, 3, 3), (1, 3, 4), (1, 3, 5), (1, 4, 4), (1, 4, 5),
  11. (1, 5, 5), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 2, 5), (2, 3, 3), (2, 3, 4),
  12. (2, 3, 5), (2, 4, 4), (2, 4, 5), (2, 5, 5), (3, 3, 3), (3, 3, 4), (3, 3, 5),
  13. (3, 4, 4), (3, 4, 5), (3, 5, 5), (4, 4, 4), (4, 4, 5), (4, 5, 5), (5, 5, 5)]
  14. >>>

5. 排列器 permutations

  1. >>> import itertools as it
  2. >>> list(it.permutations([1,2,3]))
  3. [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
  4. >>> # 数字1、2、3能组成哪些三位数?
  5. >>> [i[0]*100+i[1]*10+i[2] for i in it.permutations([1,2,3])]
  6. [123, 132, 213, 231, 312, 321]
  7. >>>

6. 压缩器 compress

按照真值表来精简迭代器,筛选出部分值

  1. >>> import itertools as it
  2. >>> i = it.compress(range(6), (1,1,0,0,1,0))
  3. >>> list(i)
  4. [0, 1, 4]
  5. >>>

7. 切片器 islice

  1. >>> import itertools as it
  2. >>> islice = it.islice(range(100),0,9,2)
  3. >>> list(islice)
  4. [0, 2, 4, 6, 8]
  5. >>> iSlice = it.islice(range(1,100),0,9,2)
  6. >>> list(iSlice)
  7. [1, 3, 5, 7, 9]
  8. >>> # 可以不指定起始和步长,直接指定个数
  9. >>> list(it.islice(range(1,100),10))
  10. [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
  11. >>>

8. 计数器 count

因为生成器只提供说法不是数据集,直接用 list(count1)会死循环的,可以用islice()指定一下个数。

  1. >>> import itertools as it
  2. >>> count1 = it.count(start=0,step=3)
  3. >>> list(it.islice(count1,12))
  4. [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33]
  5. >>> count2 = it.count(start=100,step=-2)
  6. >>> list(it.islice(count2,10))
  7. [100, 98, 96, 94, 92, 90, 88, 86, 84, 82]
  8. >>>

9. 循环器 cycle

  1. >>> import itertools as it
  2. >>> list(it.islice(it.cycle('ABC'),10))
  3. ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']
  4. >>> list(it.islice(it.cycle([1,2,3,4]),10))
  5. [1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
  6. >>>

10. 重复器 repeat

  1. >>> import itertools as it
  2. >>> list(it.repeat(5,10))
  3. [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
  4. >>> list(it.repeat([1,2],5))
  5. [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]
  6. >>>

11. 舍真器 dropwhile

舍弃不满足条件的元素,但当条件不满足即停止筛选

  1. >>> import itertools as it
  2. >>> lst = [1,3,5,2,4,6,10,11,7,8,12,15]
  3. >>> list(it.dropwhile(lambda i:i<9,lst))
  4. [10, 11, 7, 8, 12, 15]
  5. >>> list(it.dropwhile(lambda i:i%2,lst))
  6. [2, 4, 6, 10, 11, 7, 8, 12, 15]
  7. >>>

12. 留真器 takewhile

留下满足条件的元素,但当条件不满足即停止筛选

  1. >>> import itertools as it
  2. >>> list(it.takewhile(lambda i:i<6, range(10)))
  3. [0, 1, 2, 3, 4, 5]
  4. >>> lst = [1,3,5,2,4,6,10,11,7,8,12,15]
  5. >>> list(it.takewhile(lambda i:i<11,lst))
  6. [1, 3, 5, 2, 4, 6, 10]
  7. >>> list(it.takewhile(lambda i:i%6,lst))
  8. [1, 3, 5, 2, 4]
  9. >>>

13. 筛假器 filterfalse

舍弃满足条件的所有元素,留下所有不满足条件的

  1. >>> import itertools as it
  2. >>> lst = [1,3,5,2,4,6,10,11,7,8,12,15]
  3. >>> list(it.filterfalse(lambda i:i<9,lst))
  4. [10, 11, 12, 15]
  5. >>> list(it.filterfalse(lambda i:i%2,lst))
  6. [2, 4, 6, 10, 8, 12]
  7. >>>

14. 分组器 groupby

  1. >>> import itertools as it
  2. >>> group = it.groupby(range(20), lambda i:not 8<i<16)
  3. >>> for i,j in group: print(i,list(j))
  4. True [0, 1, 2, 3, 4, 5, 6, 7, 8]
  5. False [9, 10, 11, 12, 13, 14, 15]
  6. True [16, 17, 18, 19]
  7. >>>

15. 乘积器 product

  1. >>> import itertools as it
  2. >>> list(it.product('ABC',(1,2)))
  3. [('A', 1), ('A', 2), ('B', 1), ('B', 2), ('C', 1), ('C', 2)]
  4. >>> [''.join(i) for i in list(it.product('ABC','12'))]
  5. ['A1', 'A2', 'B1', 'B2', 'C1', 'C2']
  6. >>> [''.join(i) for i in list(it.product('ABCD','123'))]
  7. ['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3', 'D1', 'D2', 'D3']
  8. >>> # 第二参数: repeat = n , 默认值为1
  9. >>> list(it.product('ABC',[1],repeat=2))
  10. [('A', 1, 'A', 1), ('A', 1, 'B', 1), ('A', 1, 'C', 1),
  11. ('B', 1, 'A', 1), ('B', 1, 'B', 1), ('B', 1, 'C', 1),
  12. ('C', 1, 'A', 1), ('C', 1, 'B', 1), ('C', 1, 'C', 1)]
  13. >>> [''.join(i) for i in list(it.product('ABC',repeat=2))]
  14. ['AA', 'AB', 'AC', 'BA', 'BB', 'BC', 'CA', 'CB', 'CC']
  15. >>> [''.join(i) for i in list(it.product('ABC','ABC',repeat=2))]
  16. ['AAAA', 'AAAB', 'AAAC', 'AABA', 'AABB', 'AABC', 'AACA', 'AACB', 'AACC', 'ABAA',
  17. 'ABAB', 'ABAC', 'ABBA', 'ABBB', 'ABBC', 'ABCA', 'ABCB', 'ABCC', 'ACAA', 'ACAB',
  18. 'ACAC', 'ACBA', 'ACBB', 'ACBC', 'ACCA', 'ACCB', 'ACCC', 'BAAA', 'BAAB', 'BAAC',
  19. 'BABA', 'BABB', 'BABC', 'BACA', 'BACB', 'BACC', 'BBAA', 'BBAB', 'BBAC', 'BBBA',
  20. 'BBBB', 'BBBC', 'BBCA', 'BBCB', 'BBCC', 'BCAA', 'BCAB', 'BCAC', 'BCBA', 'BCBB',
  21. 'BCBC', 'BCCA', 'BCCB', 'BCCC', 'CAAA', 'CAAB', 'CAAC', 'CABA', 'CABB', 'CABC',
  22. 'CACA', 'CACB', 'CACC', 'CBAA', 'CBAB', 'CBAC', 'CBBA', 'CBBB', 'CBBC', 'CBCA',
  23. 'CBCB', 'CBCC', 'CCAA', 'CCAB', 'CCAC', 'CCBA', 'CCBB', 'CCBC', 'CCCA', 'CCCB',
  24. 'CCCC']
  25. >>>

16. 映射器 starmap

  1. >>> import itertools as it
  2. >>> list(it.starmap(str.isupper, 'AbCDefgH'))
  3. [True, False, True, True, False, False, False, True]
  4. >>> list(it.starmap(lambda a,b,c:a+b+c,([1,2,3],[4,5,6],[7,8,9])))
  5. [6, 15, 24]
  6. >>> list(it.starmap(lambda *a:sum(a),[range(5),range(10),range(101)]))
  7. [10, 45, 5050]
  8. >>>

17. 元组器 tee

返回多个迭代器的元组

  1. >>> import itertools as it
  2. >>> [list(i) for i in it.tee([1,2,3],3)]
  3. [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
  4. >>> it.tee([1,2,3],3)
  5. (<itertools._tee object at 0x030711A8>,
  6. <itertools._tee object at 0x03078228>,
  7. <itertools._tee object at 0x0131FFE8>)
  8. >>>

18. 打包器 zip_longest

与内置函数zip()类似,但元素个数以最长的迭代器为准

  1. >>> import itertools as it
  2. >>> list(it.zip_longest('ABCDE',range(1,4)))
  3. [('A', 1), ('B', 2), ('C', 3), ('D', None), ('E', None)]
  4. >>> list(zip('ABCDE',range(1,4)))
  5. [('A', 1), ('B', 2), ('C', 3)]
  6. >>> list(it.zip_longest('ABCDE',range(1,4),[1,2,3,4]))
  7. [('A', 1, 1), ('B', 2, 2), ('C', 3, 3), ('D', None, 4), ('E', None, None)]
  8. >>>

名字我随便起的,形像就好。看下来如何?十八兵器,样样精通了吗?其实掌握个几样“称手的”即可,何必面面俱到呢 ^_^

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/411381
推荐阅读
相关标签
  

闽ICP备14008679号