473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
Jul 6 '06 #1
1 1740

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
7
by: codeslayer | last post by:
Greetings to everyone in ‘forum-land': I have a problem that has plaguing me to no end. It is a CSS-related question, and I have not seen this question posted anywhere in forums or through...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
2
by: Girish Sahani | last post by:
hello ppl, Consider a list like . 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...
2
by: anchi.chen | last post by:
Hi People, Just wondering if any of you have ever come across any javascript examples that will allow one to drag and drop multiple items between lists? That is, users would be able to use the...
4
by: rn5a | last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5 options are shown at any given time) allows multiple selection whereas the 2nd one allows only 1 option to be selected at a time. ...
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
3
by: DarkSoul | last post by:
I have multiple ArrayLists, could be 2 or 3 or 10 lists, with multiple values in them. Now what I need to do is to get a combination of all of them. For example, if I have 3 lists with the...
18
by: Grant Edwards | last post by:
Could whoever is responsible for the gateway that is grabbing my postings off of Usenet and e-mailing them out please fix the headers in the mail messages so that I don't get the bounce messages?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.