ATLAS Offline Software
Loading...
Searching...
No Matches
combo.py
Go to the documentation of this file.
1# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2
3#
4# $Id: combo.py,v 1.3 2005-05-12 16:36:59 ssnyder Exp $
5# File: combo.py
6# Created: sss, Mar 2005, from http://aspn.activestate.com/.
7# Purpose: Generators for combinations and permutations.
8#
9
10
11"""Generators for combinations and permutations.
12
13This module contains functions for
14generating all combinations and permutations from a list of
15items. These functions return generators, so you may iterate
16over them or retrieve successive values with
17next(). Given a sequence items,
18you can iterate over the list of unique pairwise combinations with
19
20 for (i1, i2) in combo.xuniqueCombinations (items, 2):
21
22The functions contained in this module are:
23
24 combinations(items,n)
25 Yields all unique subsequences of length n from items.
26
27 all_combinations(items,n)
28 Similar, but differences in ordering are considered significant.
29 For example, all_combinations([1,2,3],2) will yield both [1,2] and [2,1].
30
31 permutations(items)
32 Yields all permutations of items.
33 Equivalent to all_combinations(items,len(items)).
34
35 selections(items,n)
36 Returns all possible ways of picking n items from
37 items, where any given item may be picked multiple times.
38"""
39
40from builtins import range
41__version__ = "1.0"
42
43"""combo.py
44Generators for calculating a) the permutations of a sequence and
45b) the combinations and selections of a number of elements from a
46sequence. Uses Python 2.2 generators.
47
48Similar solutions found also in comp.lang.python
49
50Keywords: generator, combination, permutation, selection
51
52See also: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/105962
53See also: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66463
54See also: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66465
55"""
56
57def all_combinations(items, n):
58 """Return all subsequences of length n from items.
59Differences in ordering are considered significant.
60For example, all_combinations([1,2,3],2) will yield both [1,2] and [2,1].
61
62>>> for c in all_combinations(['l','o','v','e'],2):
63... print (''.join(c))
64lo
65lv
66le
67ol
68ov
69oe
70vl
71vo
72ve
73el
74eo
75ev
76"""
77 if n==0:
78 yield []
79 else:
80 for i in range(len(items)):
81 for cc in all_combinations(items[:i]+items[i+1:],n-1):
82 yield [items[i]]+cc
83
84def combinations(items, n):
85 """Yields all unique subsequences of length n from items.
86
87>>> for uc in combinations(['l','o','v','e'],2):
88... print (''.join(uc))
89lo
90lv
91le
92ov
93oe
94ve
95"""
96 if n==0:
97 yield []
98 else:
99 for i in range(len(items)):
100 for cc in combinations(items[i+1:],n-1):
101 yield [items[i]]+cc
102
103def selections(items, n):
104 """Returns all possible ways of picking n items from
105items, where any given item may be picked multiple times.
106
107>>> for s in selections(['l','o','v','e'],2):
108... print (''.join(s))
109ll
110lo
111lv
112le
113ol
114oo
115ov
116oe
117vl
118vo
119vv
120ve
121el
122eo
123ev
124ee
125"""
126 if n==0:
127 yield []
128 else:
129 for i in range(len(items)):
130 for ss in selections(items, n-1):
131 yield [items[i]]+ss
132
133def permutations(items):
134 """ Yields all permutations of items.
135Equivalent to all_combinations(items,len(items)).
136
137>>> for p in permutations(['l','o','v','e']):
138... print (''.join(p))
139love
140loev
141lvoe
142lveo
143leov
144levo
145olve
146olev
147ovle
148ovel
149oelv
150oevl
151vloe
152vleo
153vole
154voel
155velo
156veol
157elov
158elvo
159eolv
160eovl
161evlo
162evol
163"""
164 return all_combinations(items, len(items))
165
166
167__test__ = {}
168__test__['tests'] = """
169>>> # Permutations of 'love'
170>>> print (list(map(''.join, list(permutations('done')))))
171['done', 'doen', 'dnoe', 'dneo', 'deon', 'deno', 'odne', 'oden', 'onde', 'oned', 'oedn', 'oend', 'ndoe', 'ndeo', 'node', 'noed', 'nedo', 'neod', 'edon', 'edno', 'eodn', 'eond', 'endo', 'enod']
172"""
173
174
175if __name__ == "__main__":
176 print ('PyAnalysisUtils/combo.py test')
177 import doctest
178 doctest.testmod()
selections(items, n)
Definition combo.py:103
combinations(items, n)
Definition combo.py:84
all_combinations(items, n)
Definition combo.py:57
permutations(items)
Definition combo.py:133