Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>itertools</code>is indeed your friend:</p> <pre><code>from itertools import permutations def group(iterable, n=2): return zip(*([iter(iterable)] * n)) for each in permutations([1, 2, 3, 4, 5, 6]): print map(list, group(each)) </code></pre> <p>Result:</p> <pre><code>[[1, 2], [3, 4], [5, 6]] [[1, 2], [3, 4], [6, 5]] [[1, 2], [3, 5], [4, 6]] [[1, 2], [3, 5], [6, 4]] [[1, 2], [3, 6], [4, 5]] [[1, 2], [3, 6], [5, 4]] [[1, 2], [4, 3], [5, 6]] [[1, 2], [4, 3], [6, 5]] [[1, 2], [4, 5], [3, 6]] ... </code></pre> <p><strong>[EDIT]</strong> @FrederikNS: After you clarified your question <a href="https://stackoverflow.com/questions/10568081/partitioning-a-sequence-into-sets-of-unique-pairs/10570837#10570837">and found an answer yourself</a>, here is my solution:</p> <pre><code>from itertools import combinations def setpartition(iterable, n=2): iterable = list(iterable) partitions = combinations(combinations(iterable, r=n), r=len(iterable) / n) for partition in partitions: seen = set() for group in partition: if seen.intersection(group): break seen.update(group) else: yield partition for each in setpartition([1, 2, 3, 4]): print each print for each in setpartition([1, 2, 3, 4, 5, 6]): print each </code></pre> <p>Result:</p> <pre><code>((1, 2), (3, 4)) ((1, 3), (2, 4)) ((1, 4), (2, 3)) ((1, 2), (3, 4), (5, 6)) ((1, 2), (3, 5), (4, 6)) ((1, 2), (3, 6), (4, 5)) ((1, 3), (2, 4), (5, 6)) ((1, 3), (2, 5), (4, 6)) ((1, 3), (2, 6), (4, 5)) ((1, 4), (2, 3), (5, 6)) ((1, 4), (2, 5), (3, 6)) ((1, 4), (2, 6), (3, 5)) ((1, 5), (2, 3), (4, 6)) ((1, 5), (2, 4), (3, 6)) ((1, 5), (2, 6), (3, 4)) ((1, 6), (2, 3), (4, 5)) ((1, 6), (2, 4), (3, 5)) ((1, 6), (2, 5), (3, 4)) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload