Combinations of lists

88888 Dihedral dihedral88888 at googlemail.com
Wed Oct 3 16:41:35 EDT 2012


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

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.



More information about the Python-list mailing list