Python语言之深入Python中的itertools模块
小职 2020-11-13 来源 : 阅读 572 评论 0

摘要:在Python中有一个功能强大的迭代工具包itertools,是Python自带的标准工具包之一。本篇深入介绍Python中的Itertools模块,希望对Python的学习有所帮助。

在Python中有一个功能强大的迭代工具包itertools,是Python自带的标准工具包之一。本篇深入介绍Python中的Itertools模块,希望对Python的学习有所帮助。

Python语言之深入Python中的itertools模块 


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)`一样.

 

import itertools

for item in itertools.product([1,2,3],[100,200]):

    print(item)

     

     

# 输出如下

(1, 100)

(1, 200)

(2, 100)

(2, 200)

(3, 100)

(3, 200)

permutations

 

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

 

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

 

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

 

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

 

items = ['a','b','c']

from itertools import permutations

for i in permutations(items):

    print(i) #排列组合

 

print(list(itertools.permutations('abc',3)))  

# 输出如下

('a', 'b', 'c')

('a', 'c', 'b')

('b', 'a', 'c')

('b', 'c', 'a')

('c', 'a', 'b')

('c', 'b', 'a')

[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]

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

 

items = ['a','b','c']

from itertools import permutations

for i in permutations(items,2):

    print(i) #排列组合

     

# 输出如下

('a', 'b')

('a', 'c')

('b', 'a')

('b', 'c')

('c', 'a')

('c', 'b')

combinations

 

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

 

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

 

>>> print(list(itertools.combinations('abc',3)))

[('a', 'b', 'c')]

combinations_with_replacement

 

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

 

>>> print(list(itertools.combinations_with_replacement('abc',3)))

[('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用于对列表中元素逐个累加

 

>>> import itertools

>>> x = itertools.accumulate(range(10))

>>> print(list(x))

[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

compress

 

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

 

import itertools

 

its=["a","b","c","d","e","f","g","h"]

selector=[True,False,1,0,3,False,-2,"y"]

for item in itertools.compress(its,selector):

    print (item)

     

a

c

e

g

h    

count

 

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

 

我们来看一个简单的例子

 

from itertools import count

for i in count(10): #从10开始无限循环

    if i > 20:  

        break

    else:

        print(i)

 

 

10

11

12

13

14

15

16

17

18

19

20

chain

 

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

 

import itertools

chain = itertools.chain([1, 2, 3], [4, 5, 6])

for c in chain:

   print(c)

1

2

3

4

5

6   

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

 

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

[1, 2, 3, 4, 5, 6, 7, 8]

cycle

 

import itertools

cycle = itertools.cycle([1, 2, 3])

for c in cycle:

   print(c)

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



关注“职坐标在线”(Zhizuobiao_Online)公众号,免费获取最新技术干货教程资源哦

本文由 @小职 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程