Combinations of lists

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Oct 3 16:29:11 EDT 2012


Oscar wrote:
>>> def uniquecombinations(h, m):
>>>     for ha in submultisets(h, len(h)//2):
>>>         hb = list(h)
>>>         for c in ha:
>>>             hb.remove(c)
>>>         yield [m[0] + a for a in ha] + [m[1] + b for b in hb]
>>>
>>> h = ['A', 'A', 'B', 'B']
>>> m = ['a', 'b']
>>>
>>> for x in uniquecombinations(h, m):
>>>     print(x)
>>> '''
>>>
>>> Output:
>>> ['aB', 'aB', 'bA', 'bA']
>>> ['aA', 'aB', 'bA', 'bB']
>>> ['aA', 'aA', 'bB', 'bB']

On 3 October 2012 21:15, Steen Lysgaard <boxeakasteen at gmail.com> wrote:
> Hi,
>
> thanks for your interest. Sorry for not being completely clear, yes
> the length of m will always be half of the length of h.
>
> /Steen

Then you can make the uniquecombinations function recursive. First
find the elements that go with 'a' then from the remaining elements
find those that go with 'b', then 'c' and so on.

Oscar



More information about the Python-list mailing list