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
时间: 2024-10-12 15:07:16