笛卡尔积(product):
假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2)}
>>> for i in itertools.product({'a', 'b'},{, , }):
... print(i, end=' ')
...
('a', ) ('a', ) ('a', ) ('b', ) ('b', ) ('b', ) >>> for i in itertools.product({, , },{'a', 'b'}):
... print(i, end=' ')
...
(, 'a') (, 'b') (, 'a') (, 'b') (, 'a') (, 'b')
组合:combinations (有重复)
combinations_with_replacement(去除重复)
>>> import itertools
>>> for i in itertools.combinations('abcd', ):
... print(i, end='')
...
('a', 'b')('a', 'c')('a', 'd')('b', 'c')('b', 'd')('c', 'd')
>>> for i in itertools.combinations_with_replacement('abcd', ):
... print(i, end='')
...
('a', 'a')('a', 'b')('a', 'c')('a', 'd')('b', 'b')('b', 'c')('b', 'd')('c', 'c')('c', 'd')('d', 'd')
排列:permutations
>>> import itertools
>>> for i in itertools.permutations('abcd', ):
... print(i, end='')
...
('a', 'b')('a', 'c')('a', 'd')('b', 'a')('b', 'c')('b', 'd')('c', 'a')('c', 'b')('c', 'd')('d', 'a')('d', 'b')('d', 'c')