Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python looping combinations of 8 objects into 3 groups, 3-3-2

Let's say I have a list of 8 objects, numbered that 1-8.

The objects are put into three boxes, 3 in one box, 3 in another box, 2 in the last box. By mathematics, there are 8C3*5C3=560 ways to do this. I want to loop through there 560 items. Is there any way in Python to do so?

The result should look like this:

list=['12','345',678'], ['12','346','578'], ..., etc.

Note that ['12','345','678'] and ['12','354',876'] are considered the same for this purpose.

I want to make a for-loop this list. Is there any way in Python to do so?

Here is the solution I get, but it seems ugly.

import itertools
for c1,c2 in itertools.combinations(range(8),2):
            l2=list(range(8))
            l2.pop(c2)
            l2.pop(c1)
            for c3,c4,c5 in itertools.combinations(l2,3):
                l3=l2[:]
                l3.remove(c5)
                l3.remove(c4)
                l3.remove(c3)
                c6,c7,c8=l3
                print(c1,c2,c3,c4,c5,c6,c7,c8)
like image 648
user2341726 Avatar asked May 02 '13 05:05

user2341726


People also ask

How do you calculate combinations in Python?

If we select k things from a total of n options and we don't care in what order they are, the total number of combinations (the number of different ways to do this) is: C(n,k)=(nk)=n!k! (n−k)!

How do you print all array combinations in Python?

combinations() module in Python to print all possible combinations. Given an array of size n, generate and print all possible combinations of r elements in array.

How do you get all the combinations of two lists in Python?

The unique combination of two lists in Python can be formed by pairing each element of the first list with the elements of the second list. Method 1 : Using permutation() of itertools package and zip() function. Approach : Import itertools package and initialize list_1 and list_2.


1 Answers

def F(seq, parts, indexes=None, res=[], cur=0):
    if indexes is None: # indexes to use for combinations
        indexes = range(len(seq))

    if cur >= len(parts): # base case
        yield [[seq[i] for i in g] for g in res]
        return    

    for x in combinations(indexes, r=parts[cur]):
        set_x = set(x)
        new_indexes = [i for i in indexes if i not in set_x]
        for comb in F(seq, parts, new_indexes, res=res + [x], cur=cur + 1):
            yield comb

it = F('12345678', parts=(2,3,3))
for i in range(10):
    print [''.join(g) for g in next(it)]

['12', '345', '678']
['12', '346', '578']
['12', '347', '568']
['12', '348', '567']
['12', '356', '478']
['12', '357', '468']
['12', '358', '467']
['12', '367', '458']
['12', '368', '457']
['12', '378', '456']

Another example:

for c in F('1234', parts=(2,2)):
    print [''.join(g) for g in c]

['12', '34']
['13', '24']
['14', '23']
['23', '14']
['24', '13']
['34', '12']
like image 172
jamylak Avatar answered Oct 21 '22 16:10

jamylak