python中itertools模块介绍---03

product(*iterables[,repeat]):

源代码:

def product(*args,**kwds):
    pools=map(tuple,args)*kwds.get("repeat",1)
    result=[[]]
    for pool in pools:
        result=[x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

求iterables的笛卡尔积,repeat指定重复生成序列的次数。如:

>>>a=(1,2)
>>>b=('a','b')
>>>c=product(a,b)
>>>c.next()
(1,'a')
>>>c.next()
(1,'b')
>>>c.next()
(2,'a')
>>>c.next()
(2,'b')

permutations(iterable[,r]):

创建一个迭代器,返回iterable中所有长度为r的项目序列,如果省略了r,那么序列的长度与iterable中项目数量相同:返回p中任意取r个元素做排列的元组的迭代器。如:

>>>a=permutations('abc',2)
>>>a.next() 
('a','b')
>>>a.next()
('a','c')
>>>a.next()
('b','a')
>>>a.next()
('b','c')
>>>a.next()
('c','a')
>>>a.next()
('c','b')
>>>b=permutations(range(2))
>>>b.next()
(0,1)
>>>b.next()
(1,0)

combinations(iterable,r):

创建一个迭代器,返回iterable中所有长度俄日r的自序列,返回的自序列中的项按输入的iterable中的顺序排序(不重复)

例如:

>>>a=combinations('abc',2)
>>>a.next()
('a','b')
>>>a.next()
('b','c')
>>>a.next()
('c','a')

combinations_with_replacement(iterable,r):

创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序(带重复)。

>>>a=combinations_with_replacement('abc',2)
>>>a.next()
('a','a')
>>>a.next()
('a','b')
>>>a.next()
('a','c')
>>>a.next()
('b','b')
>>>a.next()
('b','c')
>>>a.next()
('c','c')


转载于:https://my.oschina.net/935572630/blog/393381

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值