Generators for combinations and permutations.
This module contains functions for
generating all combinations and permutations from a list of
items. These functions return generators, so you may iterate
over them or retrieve successive values with
next(). Given a sequence items,
you can iterate over the list of unique pairwise combinations with
for (i1, i2) in combo.xuniqueCombinations (items, 2):
The functions contained in this module are:
combinations(items,n)
Yields all unique subsequences of length n from items.
all_combinations(items,n)
Similar, but differences in ordering are considered significant.
For example, all_combinations([1,2,3],2) will yield both [1,2] and [2,1].
permutations(items)
Yields all permutations of items.
Equivalent to all_combinations(items,len(items)).
selections(items,n)
Returns all possible ways of picking n items from
items, where any given item may be picked multiple times.
| python.combo.all_combinations |
( |
| items, |
|
|
| n ) |
Return all subsequences of length n from items.
Differences in ordering are considered significant.
For example, all_combinations([1,2,3],2) will yield both [1,2] and [2,1].
>>> for c in all_combinations(['l','o','v','e'],2):
... print (''.join(c))
lo
lv
le
ol
ov
oe
vl
vo
ve
el
eo
ev
Definition at line 57 of file combo.py.
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
| python.combo.permutations |
( |
| items | ) |
|
Yields all permutations of items.
Equivalent to all_combinations(items,len(items)).
>>> for p in permutations(['l','o','v','e']):
... print (''.join(p))
love
loev
lvoe
lveo
leov
levo
olve
olev
ovle
ovel
oelv
oevl
vloe
vleo
vole
voel
velo
veol
elov
elvo
eolv
eovl
evlo
evol
Definition at line 133 of file combo.py.
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
| python.combo.selections |
( |
| items, |
|
|
| n ) |
Returns all possible ways of picking n items from
items, where any given item may be picked multiple times.
>>> for s in selections(['l','o','v','e'],2):
... print (''.join(s))
ll
lo
lv
le
ol
oo
ov
oe
vl
vo
vv
ve
el
eo
ev
ee
Definition at line 103 of file combo.py.
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