# Re: Combinations of lists

Discussion in 'Python' started by Oscar Benjamin, Oct 3, 2012.

1. ### Oscar BenjaminGuest

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 <> 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

Oscar Benjamin, Oct 3, 2012

2. ### 88888 DihedralGuest

Oscar Benjaminæ–¼ 2012å¹´10æœˆ4æ—¥æ˜ŸæœŸå››UTC+8ä¸Šåˆ4æ™‚29åˆ†51ç§’å¯«é“ï¼š
> 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 <> 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

Lets simplify the problem as follows:

A set of m symbols [0, 1,2,3...m-1] and each symbol can occur
a pecified number of times [(0, k(0)), (1, k(1)), ....(m-1, k(m-1)].rom a list to form a list of (i, k(i)) where k(i) are all positive integers.

For example [ (0,3), (1,2), (2, 1), (3, 2)], this is easy to generate
valid numbers in base m numbers of sum(k(i)) digits.

in the final string.

88888 Dihedral, Oct 3, 2012

3. ### 88888 DihedralGuest

Oscar Benjaminæ–¼ 2012å¹´10æœˆ4æ—¥æ˜ŸæœŸå››UTC+8ä¸Šåˆ4æ™‚29åˆ†51ç§’å¯«é“ï¼š
> 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 <> 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

Lets simplify the problem as follows:

A set of m symbols [0, 1,2,3...m-1] and each symbol can occur
a pecified number of times [(0, k(0)), (1, k(1)), ....(m-1, k(m-1)].rom a list to form a list of (i, k(i)) where k(i) are all positive integers.

For example [ (0,3), (1,2), (2, 1), (3, 2)], this is easy to generate
valid numbers in base m numbers of sum(k(i)) digits.

in the final string.

88888 Dihedral, Oct 3, 2012