Find combinations of a list of sets
<p>I'm using python 2.7. Given a list of sets, is there any quick and efficient way to generate the combinations? Input sets always have one item in each set and output sets all have length of 2 <br><br>
From:</p>

<pre><code>[set(['item1']), set(['item2']), set(['item3'])]
</code></pre>

<p>To:</p>

<pre><code>[set(['item1','item2']), set(['item2','item3']), set(['item3','item1'])]
</code></pre>

<p>As John pointed out <code>itertools</code> might be of help. Here's a quick example:</p>

<pre><code>import itertools as it
sets = [set(range(0, 3)), set(range(2, 5)), set(range(4, 7))]
comb = list(it.combinations(sets, r=2))
comb
</code></pre>

<p>output: <code>[({0, 1, 2}, {2, 3, 4}), ({0, 1, 2}, {4, 5, 6}), ({2, 3, 4}, {4, 5, 6})]</code></p>

<p>Then create an intersection in each iteration:</p>

<pre><code>comb_sets = [a.intersection(b) for a, b in comb]
comb_sets
</code></pre>

<p>output: <code>[{2}, set(), {4}]</code></p>

python python-2.7 set combinations