By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

Generating multiple lists from one list

P: n/a
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
Jul 6 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

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(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/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,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

Jul 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.