Python Combinations
<p>Suppose I have a Python list that looks like:</p>

<pre><code>[1, 2, 3, 4]
</code></pre>

<p>I would like to be able to return a list of lists containing all combinations of two or more numbers. The order of the values doesn't matter so 1,2 is the same as 2,1.  I would also like to return another list that contains the values that do not feature in each combination. For example:</p>

<pre><code>Combination 1,2 / Remainder 3,4
Combination 2,3 / Remainder 1,4
Combination 1,2,3 / Remainder 4
Combination 1,2,3,4/ Remainder -
</code></pre>

<p>The returned lists for the above would be</p>

<pre><code>combination = [[1,2], [2,3], [1,2,3], [1,2,3,4]]
remainder = [[3,4], [1,4], [4], []]
</code></pre>

<p>I've only shown a few examples...</p>

<p>I realise that the first part can probably be achieved using <code>itertools.combinations</code> but how can I return those values not used in the combination without looping?</p>

<p>Suppose you have this vector [1 6 3]</p>

<p>You can generate all of the numbers from 0 to 2^3-1 where 3 is <code>len([1 6 3])</code></p>

<pre><code>0
1
2
3
4
5
6
7
</code></pre>

<p>After you can convert this numbers to binary:</p>

<pre><code> 0 0 0
 0 0 1
 0 1 0
 0 1 1
 1 0 0
 1 0 1
 1 1 0
 1 1 1
</code></pre>

<p>Put your vector at the top of the sequence generated:</p>

<pre><code>[1 6 3]
 0 0 0
 0 0 1
 0 1 0
 0 1 1
 1 0 0
 1 0 1
 1 1 0
 1 1 1
</code></pre>

<p>for each line append in combination the number that are in the same position of the 1s and in the remainder that one that are in the position of the 0s.</p>

<p>so, for instance, looking at the 4th line:</p>

<pre><code>Combination: [6,3]
Remainder: [1]
</code></pre>

<p>At the end:</p>

<pre><code>Combination: [],[3],[6],[6,3],[1],[1,3],[1,6],[1,6,3]
Remainder: [1,6,3],[1,3],[1],[6,3],[6],[3],[]
</code></pre>

<p>Here the code:</p>

<pre><code>vec=[1,3,6]
binary_vec = [format(i,'b').zfill(len(vec)) for i in range(2**len(vec))]
print([[vec[i] for i,y in enumerate(x) if y != "0"] for x in binary_vec])
print([[vec[i] for i,y in enumerate(x) if y == "0"] for x in binary_vec])
</code></pre>

<p>Output:</p>

<p><a href="http://i.stack.imgur.com/wfmPn.png" rel="nofollow"><img src="http://i.stack.imgur.com/wfmPn.png" alt="enter image description here"></a></p>

<p>Look also at my answer in this post:</p>

<p><a href="http://stackoverflow.com/questions/39428179/determine-list-of-all-possible-products-from-a-list-of-integers-in-python/39428550#39428550">Determine list of all possible products from a list of integers in Python</a></p>

python python-2.7 combinations itertools