当前位置:主页 > 查看内容

深入Python中的itertools模块

发布时间:2021-10-21 00:00| 位朋友查看

简介:在Python中有一个功能强大的迭代工具包itertools,是Python自带的标准工具包之一。 product 由于itertools是内置库,不需要任何安装,直接import itertools即可。 product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即:……

在Python中有一个功能强大的迭代工具包itertools,是Python自带的标准工具包之一。

product

由于itertools是内置库,不需要任何安装,直接import itertools即可。

product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即:

笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尔积(Cartesian product),又称直积,表示为X × Y。

product(A, B)和 ``((x,y) for x in A for y in B)`一样.

  1. import itertools 
  2. for item in itertools.product([1,2,3],[100,200]): 
  3.     print(item) 
  4.      
  5.      
  6. # 输出如下 
  7. (1, 100) 
  8. (1, 200) 
  9. (2, 100) 
  10. (2, 200) 
  11. (3, 100) 
  12. (3, 200) 

permutations

通俗地讲,permutations就是返回可迭代对象的所有数学或者字符的全排列方式。

全排列,即产生指定数目的元素的所有排列(顺序有关),也就是高中排列组合中的那个A。

permutations它接受一个集合对象,然后产生一个元组序列。

比如print(list(itertools.permutations('abc',3))),共有种情况。

  1. items = ['a','b','c'
  2. from itertools import permutations 
  3. for i in permutations(items): 
  4.     print(i) #排列组合 
  5.  
  6. print(list(itertools.permutations('abc',3)))  
  7. # 输出如下 
  8. ('a''b''c'
  9. ('a''c''b'
  10. ('b''a''c'
  11. ('b''c''a'
  12. ('c''a''b'
  13. ('c''b''a'
  14. [('a''b''c'), ('a''c''b'), ('b''a''c'), ('b''c''a'), ('c''a''b'), ('c''b''a')] 

如果需要指定长度的所有排列,可以传递一个可选的长度参数r。

  1. items = ['a','b','c'
  2. from itertools import permutations 
  3. for i in permutations(items,2): 
  4.     print(i) #排列组合 
  5.      
  6. # 输出如下 
  7. ('a''b'
  8. ('a''c'
  9. ('b''a'
  10. ('b''c'
  11. ('c''a'
  12. ('c''b'

combinations

求列表或生成器中指定数目的元素不重复的所有组合

itertools.permutations(iter,r) 和 itertools.combinations(iter,r)的区别是:前者是permutations允许重复使用,后者combinations是不能重复使用

  1. >>> print(list(itertools.combinations('abc',3))) 
  2. [('a''b''c')] 

combinations_with_replacement

combinations_with_replacement和combinations很相似,唯一的不同在于前者combinations_with_replacement集合类型中的数据是可以重复的

  1. >>> print(list(itertools.combinations_with_replacement('abc',3))) 
  2. [('a''a''a'), ('a''a''b'), ('a''a''c'), ('a''b''b'), ('a''b''c'), ('a''c''c'), ('b''b''b'), ('b''b''c'), ('b''c''c'), ('c''c''c')] 

accumulate

accumulate用于对列表中元素逐个累加

  1. >>> import itertools 
  2. >>> x = itertools.accumulate(range(10)) 
  3. >>> print(list(x)) 
  4. [0, 1, 3, 6, 10, 15, 21, 28, 36, 45] 

compress

compress()是筛选工具,它接受一个可迭代对象以及一个布尔选择序列作为输入,输出时会将所有布尔序列中为True的可迭代对象输出。

  1. import itertools 
  2.  
  3. its=["a","b","c","d","e","f","g","h"
  4. selector=[True,False,1,0,3,False,-2,"y"
  5. for item in itertools.compress(its,selector): 
  6.     print (item) 
  7.      
  8. h    

count

count(初值=0, 步长=1)是 创建一个迭代器,从传入的起始参数开始的均匀间隔的数值。

我们来看一个简单的例子

  1. from itertools import count 
  2. for i in count(10): #从10开始无限循环 
  3.     if i > 20:  
  4.         break 
  5.     else
  6.         print(i) 
  7.  
  8.  
  9. 10 
  10. 11 
  11. 12 
  12. 13 
  13. 14 
  14. 15 
  15. 16 
  16. 17 
  17. 18 
  18. 19 
  19. 20 

chain

chain链条,主要用来把多个序列连在一起做迭代。

  1. import itertools 
  2. chain = itertools.chain([1, 2, 3], [4, 5, 6]) 
  3. for c in chain: 
  4.    print(c) 
  5. 6   

chain还有一个非常重要的功能就是展平列表。

  1. >>> list(itertools.chain([1, 2, 3], [4, 5], [6] ,[7,8])) 
  2. [1, 2, 3, 4, 5, 6, 7, 8] 

cycle

  1. import itertools 
  2. cycle = itertools.cycle([1, 2, 3]) 
  3. for c in cycle: 
  4.    print(c) 

运行结果输出 1 2 3 1 2 3……一直周而复始,永不停息。


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/lY0ZBCVhup060jcTgFg5Gw
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:带你深入了解GitLab CI/CD原理及流程 下一篇:没有了

推荐图文


随机推荐