Generating multiple lists from one list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Girish Sahani

    #1

    Generating multiple lists from one list

    hello ppl,

    Consider a list like ['a.1','b.3','b. 4','c.2']. Here 'a','b','c' are
    objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1
    and b.1 cannot exist together. From this list i want to generate
    multiple lists such that each list must have one and only one instance of
    every object.
    Thus, for the above list, my output should be:
    [['a.1','b.3','c. 2'],['a.1','b.4','c. 2']]
    Another example: Let l = ['a.1','b.3','b. 4','c.2','c.6', 'd.3']. Then
    output should be [['a.1','b.3','c. 2','d.3'],['a.1','b.3','c. 6','d.3'],
    ['a.1','b.4','c. 2','d.3'],[['a.1','b.4','c. 6','d.3']

    Can anyone suggest me a time-efficient method for doing this??

    TIA,
    girish


  • Gerard Flanagan

    #2
    Re: Generating multiple lists from one list


    Girish Sahani wrote:
    hello ppl,
    >
    Consider a list like ['a.1','b.3','b. 4','c.2']. Here 'a','b','c' are
    objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1
    and b.1 cannot exist together. From this list i want to generate
    multiple lists such that each list must have one and only one instance of
    every object.
    Thus, for the above list, my output should be:
    [['a.1','b.3','c. 2'],['a.1','b.4','c. 2']]
    Another example: Let l = ['a.1','b.3','b. 4','c.2','c.6', 'd.3']. Then
    output should be [['a.1','b.3','c. 2','d.3'],['a.1','b.3','c. 6','d.3'],
    ['a.1','b.4','c. 2','d.3'],[['a.1','b.4','c. 6','d.3']
    >
    Can anyone suggest me a time-efficient method for doing this??
    >

    I don't understand what you mean by "'a','b','c ' are objects and
    1,3,4,2 are their instance ids", but I think the solution to whatever
    your problem is, will involve the Cartesian Product of sets (lists) -
    (also called the cross product).

    If your data is just the strings that you give in your example, then
    the following should work. If your 'object strings' are longer than one
    character, you will have to adapt it.


    print
    import itertools as it

    data1 = ['a.1','b.3','b. 4','c.2']
    data2 = ['a.1','b.3','b. 4','c.2','c.6', 'd.3']

    want1 = [['a.1', 'b.3', 'c.2'],
    ['a.1', 'b.4', 'c.2']]
    want2 = [['a.1','b.3','c. 2','d.3'],
    ['a.1','b.3','c. 6','d.3'],
    ['a.1','b.4','c. 2','d.3'],
    ['a.1','b.4','c. 6','d.3'] ]

    def split_data(data ):
    ret = []
    for k, g in it.groupby(sort ed(data), lambda x: x[0]):
    ret.append( list(g) )
    return ret

    #following function from ASPN Cookbook by David Klaffenbach
    #http://aspn.activestate.com/ASPN/Coo.../Recipe/302478
    def combine(*seqin) :
    '''returns a list of all combinations of argument sequences.
    for example: combine((1,2),( 3,4)) returns
    [[1, 3], [1, 4], [2, 3], [2, 4]]'''
    def rloop(seqin,lis tout,comb):
    '''recursive looping function'''
    if seqin: # any more sequences to
    process?
    for item in seqin[0]:
    newcomb=comb+[item] # add next item to current comb
    # call rloop w/ rem seqs, newcomb
    rloop(seqin[1:],listout,newcom b)
    else: # processing last sequence
    listout.append( comb) # comb finished, add to list
    listout=[] # listout initialization
    rloop(seqin,lis tout,[]) # start recursive process
    return listout


    assert combine(*split_ data(data1)) == want1
    assert combine(*split_ data(data2)) == want2

    --------------------------------

    Gerard

    Comment

    Working...