itertools之排列组合迭代器(Combinatoric Iterators)

一、itertools.product

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

注解:
Cartesian product of input iterables.
Roughly equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B).

个人理解:顾名思义,product是乘积的意思,而它是以笛卡尔乘积的形式去组合多个iterable对象里面的元素,并以元组的形式输出。如果你想用单个的iterable对象对它自身求取笛卡尔积的组合,那么你可以通过参数repeat去指定使用几个相同的iterable对象求笛卡尔积组合。

举个例子:

for i in itertools.product('ABC', 'xy'):
	print(i)
	time.sleep(0.5)

# 输出结果:  
('A', 'x')
('A', 'y')
('B', 'x')
('B', 'y')
('C', 'x')
('C', 'y')

# 如果将第一行代码改为:for i in itertools.product('AB', repeat=3)那么输出的结果为:  
('A', 'A', 'A')
('A', 'A', 'B')
('A', 'B', 'A')
('A', 'B', 'B')
('B', 'A', 'A')
('B', 'A', 'B')
('B', 'B', 'A')
('B', 'B', 'B')  

二、itertools.permutations

语法:itertools.permutations(iterable, r=None)

注解:
Return successive r length permutations of elements in the iterable.
If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated.

个人理解:这个方法能将传入的iterable对象里面的元素以r的长度进行排列组合,特点是,每个元素不会与自身进行排列组合

举个例子:

for i in itertools.permutations('ABC', 2):
	print(i)
	time.sleep(0.5)  

# 输出结果为:  
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'C')
('C', 'A')
('C', 'B')

三、itertools.combinations

语法:itertools.combinations(iterable, r)

注解:
Return r length subsequences of elements from the input iterable.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.

个人理解:此方法能将传入的iterable对象里面的元素按照r的长度进行排列组合,特点是,每个元素都是相对位置是唯一的,并不是按照元素的值去唯一区分,所以,如果传入的iterable对象里面的每个元素都是值唯一的,这样的排列组合中是不会存在元素重复的组合,例如:(‘A’, ‘B’)和(‘B’, ‘A’)不会同时出现,(‘A’, ‘A’),(‘B’, ‘B’), (‘C’, ‘C’)…也不会出现。

举个例子:

for i in itertools.combinations('ABC', 2):  
	print(i)
	time.sleep(0.5)

# 输出结果为:  
('A', 'B')
('A', 'C')
('B', 'C')  

# 如果将第一行代码改为:for i in itertools.combinations('AAC', 2)则输出结果为:  
('A', 'A')
('A', 'C')
('A', 'C')  

四、itertools.combinations_with_replacement

语法:itertools.combinations_with_replacement(iterable, r)

注解:
Return r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, the generated combinations will also be unique.

个人理解:此处类似于itertools.combinations,但是唯一的区别在于这个方法允许元素自身和自身形成排列组合

举个例子:

for i in itertools.combinations_with_replacement('ABC', 2):  
	print(i)
	time.sleep(0.5)  

# 结果输出为:  
('A', 'A')
('A', 'B')
('A', 'C')
('B', 'B')
('B', 'C')
('C', 'C')  

总结

基本上itertools提供的这四种方法能解决绝大多数需要排列组合的场景,各位同仁可以按需使用,更多详细信息请查阅官方文档:
https://docs.python.org/3.6/library/itertools.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值