Reduce execution time of combinations-producing script

Viewed 108

I need to reduce time of executing my python's script which generate a list of some combinations. A brief explanation of the problem:

There are two lists:

char_list = ['a','b','c','d','e','f','g','h']
n_list = [3,2,1,2]

The goal is to create one collection (list, tuple or whatever you want) of all possible combinations of characters from char_list with length and order according to pattern in n_list. One example out of 1680 possible:

(('a', 'd', 'e'), ('h', 'c'), ('b',), ('d', 'f'))

All combinations in collection must look like this one above, the only thing which will be changing is the place of particular characters. And this is where the difficulties begin because there are some rules which cannot be omitted:

  • there can't be duplicates of characters in each combination (each character must occur only once in combination)
  • combinations with changed order of characters in tuples which are on the same place as the previous ones also are treated as duplicates (this rule is more complicated so let me show you example):

let's say we have thousands of combinations in our collection and suddenly we notice four that are looking almost same:

(('a', 'c', 'e'), ('b', 'd'), ('g',), ('f', 'h'))
(('a', 'c', 'e'), ('h', 'f'), ('g',), ('d', 'b'))
(('a', 'c', 'e'), ('f', 'h'), ('g',), ('b', 'd'))
(('a', 'e', 'c'), ('h', 'f'), ('g',), ('d', 'b'))

only two of them are correct (can belong to our collection, btw this situation means that the entire collection is wrong because among these four combinations there are two wrong) Which ones? First one is fine (at least for the purpose of this example) but in the case of the next three only one of them is fine and this is the first one (first among these three, second if we count from the beginning of all 4 combinations) because it show up before the next two in whole collection. Why third and fourth combination is not unique? Because the place of tuples with particular order of characters within them hasn't changed; only characters switched places but only in particular tuples and this not what makes whole combination unique. Look once again at the tuples of the first and third combinations. They are same. But the order of these tuples is different. The one (order) of the first one is unique in regard to others. My approach to this coding problem:

import itertools as iter

char_list = ['a','b','c','d','e','f','g','h']
n_list = [3,2,1,2]

###this line creates a list of all possible combinations of characters within tuples###
char_comb_in_tuples = list(iter.chain(*[list(iter.combinations(char_list,n)) for n in n_list]))
### this is list in which the appropriate combinations will be appended###
list_of_good_combinations = []
###for loop for looping over all possible combinations of tuples from 'char_comb_in_tuples'###
for combination in iter.combinations(char_comb_in_tuples,4):
###filtering only these combinations with appropriate pattern from n_list (3,2,1,2)###
    if len([tuple for n_list_number, tuple in zip(n_list, combination) if n_list_number ==len(tuple)])==4:
###filtering only these combinations with no character duplicates###
        if len(list(iter.chain(*combination))) != len(set(list(iter.chain(*combination)))):
            pass
        else:
###appending right combination to final list###
            list_of_good_combinations.append(combination)
    else:
        pass
1 Answers

You can use a recursive function that picks the number of items from the list specified in the first of the given partitions with itertools.combinations, subtract the picked items from the item pool, pass them to the next recursive call with the rest of the partitions, and merge the returning combinations with each of the currently picked combination for the first partition. For efficient subtraction of items from a pool, you can convert the given list to a set first:

from itertools import combinations

def partitioned_combinations(s, partitions):
    if partitions:
        for combination in combinations(s, r=partitions[0]):
            for combinations in partitioned_combinations(s.difference(combination), partitions[1:]):
                yield (combination, *combinations)
    else:
        yield ()

so that:

list(partitioned_combinations(set(char_list), n_list))

would return 1680 tuples in a list:

[(('a', 'e', 'f'), ('c', 'd'), ('g',), ('h', 'b')),
 (('a', 'e', 'f'), ('c', 'd'), ('b',), ('h', 'g')),
 (('a', 'e', 'f'), ('c', 'd'), ('h',), ('g', 'b')),
 (('a', 'e', 'f'), ('c', 'g'), ('d',), ('b', 'h')),
 (('a', 'e', 'f'), ('c', 'g'), ('b',), ('d', 'h')),
 (('a', 'e', 'f'), ('c', 'g'), ('h',), ('d', 'b')),
 (('a', 'e', 'f'), ('c', 'b'), ('d',), ('g', 'h')),
 (('a', 'e', 'f'), ('c', 'b'), ('g',), ('d', 'h')),
 (('a', 'e', 'f'), ('c', 'b'), ('h',), ('d', 'g')),
 (('a', 'e', 'f'), ('c', 'h'), ('d',), ('g', 'b')),
 ...

Note that sets are unordered in Python so the result of this approach will not be in a definite order. If you do want them to be in order, however, you can install the ordered-set module, so that:

from ordered_set import OrderedSet
list(partitioned_combinations(OrderedSet(char_list), n_list))

returns:

[(('a', 'b', 'c'), ('d', 'e'), ('f',), ('g', 'h')),
 (('a', 'b', 'c'), ('d', 'e'), ('g',), ('f', 'h')),
 (('a', 'b', 'c'), ('d', 'e'), ('h',), ('f', 'g')),
 (('a', 'b', 'c'), ('d', 'f'), ('e',), ('g', 'h')),
 (('a', 'b', 'c'), ('d', 'f'), ('g',), ('e', 'h')),
 (('a', 'b', 'c'), ('d', 'f'), ('h',), ('e', 'g')),
 (('a', 'b', 'c'), ('d', 'g'), ('e',), ('f', 'h')),
 (('a', 'b', 'c'), ('d', 'g'), ('f',), ('e', 'h')),
 (('a', 'b', 'c'), ('d', 'g'), ('h',), ('e', 'f')),
 (('a', 'b', 'c'), ('d', 'h'), ('e',), ('f', 'g')),
...
Related