473,396 Members | 2,029 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,396 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 1745

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.