473,770 Members | 3,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1758

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

Jul 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4830
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 ordered by my instructor and she does have her reasons so I have to use char*. So there is alot of c in the code as well Anyways, I have a linked list of linked lists of a class we defined, I need to make all this into a char*, I know that I...
7
6846
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 Google search. So here is my problem: I want to use CSS to apply images as bullet styles. However, I want to be able to apply VARIOUS, MULTIPLE styles in the same document. For example, let's assume I have a set of categorized hyperlinks on a...
3
472
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 your assitance in helping me solve this problem. Information: Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like
2
310
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 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: ,] Another example: Let l = . Then
2
8154
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 "Shift" or "Ctrl" buttons to select multiple items from LIST 1 and drag them to LIST 2 in one go. I've already have the code to drag and drop between lists but for 1 item at a time. The only method to move multiple items between lists
4
4400
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. When an option is selected in the 2nd select list, the ASP page posts itself. Assume that the 1st select list has the following 10 options (note that both the select lists are actually populated from 2 different database tables): Australia
11
2250
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 to illustrate this. Problem: I need to expose two lists of objects from a high-level class. I would like to expose these lists as read-only, but require write access internally.
3
12776
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 following values: List 1: 3, 5, 7 List 2: 3, 5, 6 List 3: 2, 9
18
2388
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? While you're at it, might as well fix it for everybody else too. ;) Its a bit rude to send out mass e-mail messages with headers faked up so that the bounce messages go to somebody else.
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.