3

I want to make a combination of 1's and 0's in a 2d array like the following:

[[ 1, 1, 1, 1, 0, 0, 0, 0 ],
 [ 1, 1, 1, 0, 1, 0, 0, 0 ],
 [ 1, 1, 1, 0, 0, 1, 0, 0 ],
 [ 1, 1, 1, 0, 0, 0, 1, 0 ],
 [ 1, 1, 1, 0, 0, 0, 0, 1 ],
 .
 .
 .
]

That means a combination of four 1's and four 0's. I have looked at the itertools module's permutations() and combinations(), but couldn't find any suitable functions to perform this combination.

1

2 Answers 2

7

You can also use combinations to generate unique combinations directly:

n = 8
n1 = 4
for x in itertools.combinations( xrange(n), n1 ) :
    print [ 1 if i in x else 0 for i in xrange(n) ] 

[1, 1, 1, 1, 0, 0, 0, 0]
[1, 1, 1, 0, 1, 0, 0, 0]
[1, 1, 1, 0, 0, 1, 0, 0]
[1, 1, 1, 0, 0, 0, 1, 0]
...
[0, 0, 0, 1, 1, 1, 0, 1]
[0, 0, 0, 1, 1, 0, 1, 1]
[0, 0, 0, 1, 0, 1, 1, 1]
[0, 0, 0, 0, 1, 1, 1, 1]

This is more efficient than permutations because you don't iterate over unwanted solutions.

The intuition is that you're trying to find all possible ways to fit four "1"s in a sequence of length 8; that's the exact definition of combinations. That number is C(8,4)=8! / (4! * 4!) = 70. In contrast, the solution using permutations iterates over 8! = 40,320 candidate solutions.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 However, I'd still prefer the permutations solution, particularly the one-liner, as it is immediately clear what the code does, and the generation-overhead is not that big either (but might be for more bits).
That's the thing I wanted. Thanks a lot usual me.
@tobias_k: the solution using permutations might be more readable, but try to use it with n>15...
Ah, found the term: multiset permutation.
@usualme Yes, that's basically what I wanted to express in my comment. ;-)
1

You are producing a permutation of a multiset.

The simple but naive approach is to use itertools.permutations(), with a set to filter out repeated combinations:

>>> from itertools import permutations
>>> seen = set()
>>> for combo in permutations([1] * 4 + [0] * 4):
...     if combo not in seen:
...         seen.add(combo)
...         print combo
...
(1, 1, 1, 1, 0, 0, 0, 0)
(1, 1, 1, 0, 1, 0, 0, 0)
(1, 1, 1, 0, 0, 1, 0, 0)
(1, 1, 1, 0, 0, 0, 1, 0)
(1, 1, 1, 0, 0, 0, 0, 1)
(1, 1, 0, 1, 1, 0, 0, 0)
# ...
(0, 0, 1, 0, 1, 0, 1, 1)
(0, 0, 1, 0, 0, 1, 1, 1)
(0, 0, 0, 1, 1, 1, 1, 0)
(0, 0, 0, 1, 1, 1, 0, 1)
(0, 0, 0, 1, 1, 0, 1, 1)
(0, 0, 0, 1, 0, 1, 1, 1)
(0, 0, 0, 0, 1, 1, 1, 1)

or, producing the whole sequence in one go:

set(permutations([1] * 4 + [0] * 4))

but this loses the order that permutations produced.

The set is needed as permutations() sees the 4 1 and 4 0 as individual characters and one 1 swapped with another is considered a unique permutation.

You could also use the ordering in the sequence to avoid having to use a set:

last = (1,) * 8
for combo in permutations([1] * 4 + [0] * 4):
    if combo < last:
        last = combo
        print combo

The approach is naive in that 2n! permutations are produced where we only want (2n choose n) elements. For your case that's 40320 permutations where we only need to produce 70.

6 Comments

Or just set(itertools.permutations([1]*4+[0]*4))?
@tobias_k: Yup, but then you are forced to produce the whole set in one go, and order might be important.
Since the set has only 70 elements, I don't think this is much of a problem, neither is sorting the set like sorted(set(...), reverse=True). +1
Thanks tobias_k. It is done. But how can I formulate it like a matrix as I have given the question. Like [[],[],[]] format
@user3812253: permutations() produces tuples; you can easily call list() on each not-yet-seen result and add that to a list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.