码迷,mamicode.com
首页 > 编程语言 > 详细

python中itertools模块介绍---03

时间:2015-03-29 23:49:12      阅读:546      评论:0      收藏:0      [点我收藏+]

标签:

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)----->‘ab‘,‘ac‘,‘bc‘,‘ba‘,‘ca‘,‘cb‘ 
>>>b=permutations(range(2))----->01 10


python中itertools模块介绍---03

标签:

原文地址:http://my.oschina.net/935572630/blog/393381

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!