ATLAS Offline Software
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 from __future__ import print_function
11 
12 """Generators for combinations and permutations.
13 
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
20 
21  for (i1, i2) in combo.xuniqueCombinations (items, 2):
22 
23 The functions contained in this module are:
24 
25  combinations(items,n)
26  Yields all unique subsequences of length n from items.
27 
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].
31 
32  permutations(items)
33  Yields all permutations of items.
34  Equivalent to all_combinations(items,len(items)).
35 
36  selections(items,n)
37  Returns all possible ways of picking n items from
38  items, where any given item may be picked multiple times.
39 """
40 
41 from builtins import range
42 __version__ = "1.0"
43 
44 """combo.py
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.
48 
49 Similar solutions found also in comp.lang.python
50 
51 Keywords: generator, combination, permutation, selection
52 
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
56 """
57 
58 def all_combinations(items, n):
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].
62 
63 >>> for c in all_combinations(['l','o','v','e'],2):
64 ... print (''.join(c))
65 lo
66 lv
67 le
68 ol
69 ov
70 oe
71 vl
72 vo
73 ve
74 el
75 eo
76 ev
77 """
78  if n==0:
79  yield []
80  else:
81  for i in range(len(items)):
82  for cc in all_combinations(items[:i]+items[i+1:],n-1):
83  yield [items[i]]+cc
84 
85 def combinations(items, n):
86  """Yields all unique subsequences of length n from items.
87 
88 >>> for uc in combinations(['l','o','v','e'],2):
89 ... print (''.join(uc))
90 lo
91 lv
92 le
93 ov
94 oe
95 ve
96 """
97  if n==0:
98  yield []
99  else:
100  for i in range(len(items)):
101  for cc in combinations(items[i+1:],n-1):
102  yield [items[i]]+cc
103 
104 def selections(items, n):
105  """Returns all possible ways of picking n items from
106 items, where any given item may be picked multiple times.
107 
108 >>> for s in selections(['l','o','v','e'],2):
109 ... print (''.join(s))
110 ll
111 lo
112 lv
113 le
114 ol
115 oo
116 ov
117 oe
118 vl
119 vo
120 vv
121 ve
122 el
123 eo
124 ev
125 ee
126 """
127  if n==0:
128  yield []
129  else:
130  for i in range(len(items)):
131  for ss in selections(items, n-1):
132  yield [items[i]]+ss
133 
134 def permutations(items):
135  """ Yields all permutations of items.
136 Equivalent to all_combinations(items,len(items)).
137 
138 >>> for p in permutations(['l','o','v','e']):
139 ... print (''.join(p))
140 love
141 loev
142 lvoe
143 lveo
144 leov
145 levo
146 olve
147 olev
148 ovle
149 ovel
150 oelv
151 oevl
152 vloe
153 vleo
154 vole
155 voel
156 velo
157 veol
158 elov
159 elvo
160 eolv
161 eovl
162 evlo
163 evol
164 """
165  return all_combinations(items, len(items))
166 
167 
168 __test__ = {}
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']
173 """
174 
175 
176 if __name__ == "__main__":
177  print ('PyAnalysisUtils/combo.py test')
178  import doctest
179  doctest.testmod()
python.combo.all_combinations
def all_combinations(items, n)
Definition: combo.py:58
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.combo.permutations
def permutations(items)
Definition: combo.py:134
python.combo.selections
def selections(items, n)
Definition: combo.py:104
python.combo.combinations
def combinations(items, n)
Definition: combo.py:85