Re: Combinations of lists

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

  1. 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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    425
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. breal
    Replies:
    14
    Views:
    953
    Steven D'Aprano
    Jan 17, 2008
  3. Ed W.
    Replies:
    1
    Views:
    138
    Jürgen Exner
    Oct 22, 2003
  4. Steen Lysgaard

    Combinations of lists

    Steen Lysgaard, Oct 3, 2012, in forum: Python
    Replies:
    4
    Views:
    173
    Ian Kelly
    Oct 3, 2012
  5. Oscar Benjamin

    Re: Combinations of lists

    Oscar Benjamin, Oct 3, 2012, in forum: Python
    Replies:
    0
    Views:
    184
    Oscar Benjamin
    Oct 3, 2012
Loading...

Share This Page