python itertools 组合_Python itertools迭代器——复合迭代器,pythonitertools,组合

概念:

组合迭代器:对序列的排列组合,求序列的笛卡尔积,python itertools提供了4中组合迭代器方法。

案例:

1、itertools.product(*iterables,repeat=1):

求笛卡尔积,相当于嵌套的for循环,如product(A,B)其实就是等于((x,y) for x in A for y in B ))。

每次迭代时将最右侧的元素向后迭代。

repeat设置重复迭代次数,默认是1,例如product(A,repeat=4) 等于product(A,A,A,A)。

代码:

#itertools.product()

print (list(itertools.product('AB','xy')))

print (list((x,y) for x in 'AB' for y in 'xy'))

输出:

[('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y')]

[('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y')]

代码:

#itertools.product()

print (list(itertools.product('AB','xy',repeat=2)))

print(list(itertools.product(range(2),repeat=3)))

输出:

[('A', 'x', 'A', 'x'), ('A', 'x', 'A', 'y'), ('A', 'x', 'B', 'x'), ('A', 'x', 'B', 'y'),

('A', 'y', 'A', 'x'), ('A', 'y', 'A', 'y'), ('A', 'y', 'B', 'x'), ('A', 'y', 'B', 'y'),

('B', 'x', 'A', 'x'), ('B', 'x', 'A', 'y'), ('B', 'x', 'B', 'x'), ('B', 'x', 'B', 'y'),

('B', 'y', 'A', 'x'), ('B', 'y', 'A', 'y'), ('B', 'y', 'B', 'x'), ('B', 'y', 'B', 'y')]

[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

代码:

#itertools.product()

a = (1,2,3)

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

c = itertools.product(a,b)

for i in c:

print (i)

输出:

(1, 'a')

(1, 'b')

(1, 'c')

(2, 'a')

(2, 'b')

(2, 'c')

(3, 'a')

(3, 'b')

(3, 'c')

代码:

#itertools.product()

print (list(itertools.product((0,1),(0,1),(0,1))))

print (list(itertools.product('ab',repeat=2)))

输出:

[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

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

代码:

#itertools.product()

for i,j in itertools.product(range(2),range(2)):

print ('i = %s' %i)

print ('j = %s' %j)

for x in range(2):

for y in range(2):

print ('x = %s'% x,'y = %s'% y)

输出:

i = 0

j = 0

i = 0

j = 1

i = 1

j = 0

i = 1

j = 1

x = 0 y = 0

x = 0 y = 1

x = 1 y = 0

x = 1 y = 1

2、itertools.permutations(iterable,r=None):

连续返回由iterable元素生成长度为r的排列。

如果r=None或者省略,则返回的序列长度与iterable中的项目数量相同。

元素的值相同,位置不同也认为这些元素是不同的。

#itertools.permutations()

print (list(itertools.permutations('ABCD',2)))

print (list(itertools.permutations(range(3),3)))

结果:

[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C')]

[(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]

3、itertools.combinations(iterable,r):

返回由itertable组成长度为r的子序列。

子序列的项由输入的iterable中的顺序排序(不带重复)

#itertools.combinations()

print (list(itertools.combinations('ABCD',2)))

print (list(itertools.combinations(range(3),3)))

输出:

[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]

[(0, 1, 2)]

4、itertools.combinations_with_replacement(iterable,r)

返回由itertable组成长度为r的子序列。

子序列的项由输入的iterable中的顺序排序(带重复)

print (list(itertools.combinations_with_replacement('ABCD',2)))

结果:

[('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'B'), ('B', 'C'), ('B', 'D'), ('C', 'C'), ('C', 'D'), ('D', 'D')]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值