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')