10 from __future__
import print_function
12 """Generators for combinations and permutations.
14 This module contains functions for
15 generating all combinations and permutations from a list of
16 items. These functions return generators, so you may iterate
17 over them or retrieve successive values with
18 next(). Given a sequence items,
19 you can iterate over the list of unique pairwise combinations with
21 for (i1, i2) in combo.xuniqueCombinations (items, 2):
23 The functions contained in this module are:
26 Yields all unique subsequences of length n from items.
28 all_combinations(items,n)
29 Similar, but differences in ordering are considered significant.
30 For example, all_combinations([1,2,3],2) will yield both [1,2] and [2,1].
33 Yields all permutations of items.
34 Equivalent to all_combinations(items,len(items)).
37 Returns all possible ways of picking n items from
38 items, where any given item may be picked multiple times.
41 from builtins
import range
45 Generators for calculating a) the permutations of a sequence and
46 b) the combinations and selections of a number of elements from a
47 sequence. Uses Python 2.2 generators.
49 Similar solutions found also in comp.lang.python
51 Keywords: generator, combination, permutation, selection
53 See also: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/105962
54 See also: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66463
55 See also: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66465
59 """Return all subsequences of length n from items.
60 Differences in ordering are considered significant.
61 For example, all_combinations([1,2,3],2) will yield both [1,2] and [2,1].
63 >>> for c in all_combinations(['l','o','v','e'],2):
64 ... print (''.join(c))
81 for i
in range(len(items)):
86 """Yields all unique subsequences of length n from items.
88 >>> for uc in combinations(['l','o','v','e'],2):
89 ... print (''.join(uc))
100 for i
in range(len(items)):
105 """Returns all possible ways of picking n items from
106 items, where any given item may be picked multiple times.
108 >>> for s in selections(['l','o','v','e'],2):
109 ... print (''.join(s))
130 for i
in range(len(items)):
135 """ Yields all permutations of items.
136 Equivalent to all_combinations(items,len(items)).
138 >>> for p in permutations(['l','o','v','e']):
139 ... print (''.join(p))
169 __test__[
'tests'] =
"""
170 >>> # Permutations of 'love'
171 >>> print (list(map(''.join, list(permutations('done')))))
172 ['done', 'doen', 'dnoe', 'dneo', 'deon', 'deno', 'odne', 'oden', 'onde', 'oned', 'oedn', 'oend', 'ndoe', 'ndeo', 'node', 'noed', 'nedo', 'neod', 'edon', 'edno', 'eodn', 'eond', 'endo', 'enod']
176 if __name__ ==
"__main__":
177 print (
'PyAnalysisUtils/combo.py test')