codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
import collections from operator import itemgetter as ind data = ((1, 'Size', 'Small'), (2, 'Size', 'Large'), (3, 'Color', 'Red'), (4, 'Color', 'Green'), (5, 'Color', 'Blue'), (6, 'Shape', 'Cube'), (7, 'Shape', 'Sphere'), (8, 'Shape', 'Pyramid')) attributes = collections.defaultdict(lambda: []) for id, attribute, value in data: attributes[attribute] += [(id, value)] attributes = sorted(attributes.iteritems()) def permute(undone): if not undone: yield [] return attribute = undone[0][0] for id, value in undone[0][1]: for permutation in permute(undone[1:]): yield [(id, attribute, value)] + permutation permutations = sorted(map(sorted, permute(attributes))) for p in permutations: print ','.join(map(str, map(ind(0), p))), print ' (%s)' % ', '.join(map(ind(2), p))
Private
[
?
]
Run code