parent
9e7eeca3a2
commit
582c7f25aa
1 changed files with 43 additions and 0 deletions
@ -0,0 +1,43 @@ |
||||
# -*- python -*- |
||||
# |
||||
# $Id: generators.py,v 1.1 2010-02-19 18:41:28 wirawan Exp $ |
||||
# |
||||
# Created: 20100218 |
||||
# Wirawan Purwanto |
||||
# |
||||
""" |
||||
|
||||
Various generators |
||||
|
||||
""" |
||||
|
||||
def all_combinations(seq): |
||||
"""Generates stream of tuples containing all possible |
||||
combinations of items (where order matters). |
||||
|
||||
This is useful e.g. for generating all possible indices for a multidimensional |
||||
array. |
||||
Example: |
||||
>>> for i in all_combinations((xrange(4), xrange(3))): print i |
||||
(0, 0) |
||||
(1, 0) |
||||
(2, 0) |
||||
(3, 0) |
||||
(0, 1) |
||||
(1, 1) |
||||
(2, 1) |
||||
(3, 1) |
||||
(0, 2) |
||||
(1, 2) |
||||
(2, 2) |
||||
(3, 2) |
||||
|
||||
""" |
||||
if len(seq) <= 1: |
||||
for i in seq[0]: |
||||
yield (i,) |
||||
else: |
||||
for s1 in all_combinations(seq[1:]): |
||||
for s0 in seq[0]: |
||||
yield (s0,) + s1 |
||||
|
Loading…
Reference in new issue