Generating multiple lists from one list

G

Girish Sahani

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
 
G

Gerard Flanagan

Girish said:
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(sorted(data), lambda x: x[0]):
ret.append( list(g) )
return ret

#following function from ASPN Cookbook by David Klaffenbach
#http://aspn.activestate.com/ASPN/Cookbook/Python/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,listout,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,newcomb)
else: # processing last sequence
listout.append(comb) # comb finished, add to list
listout=[] # listout initialization
rloop(seqin,listout,[]) # start recursive process
return listout


assert combine(*split_data(data1)) == want1
assert combine(*split_data(data2)) == want2

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

Gerard
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top