Manipulating a list to generate multiple lists

Thread Solved

Join Date: May 2006
Posts: 23
Reputation: girish_sahani is an unknown quantity at this point 
Solved Threads: 0
girish_sahani girish_sahani is offline Offline
Newbie Poster

Manipulating a list to generate multiple lists

 
0
  #1
Jul 5th, 2006
hi 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 (non brute force) method for doing this??

TIA,
girish
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,022
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Manipulating a list to generate multiple lists

 
0
  #2
Jul 7th, 2006
What is your present solution, even if brute force?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 23
Reputation: girish_sahani is an unknown quantity at this point 
Solved Threads: 0
girish_sahani girish_sahani is offline Offline
Newbie Poster

Re: Manipulating a list to generate multiple lists

 
0
  #3
Jul 10th, 2006
hi,
Please look at the following:
  1. def flatten(l):
  2. for y in l:
  3. l = d.get(y.split('.')[0], [])
  4. l.append(y)
  5. d[y.split('.')[0]] = l
  6. output = [[]]
  7. for l in d.values():
  8. for i in range(len(l)-1):
  9. for j in output[:]:
  10. k = j[:]
  11. output.append(k)
  12. for i in range(0, len(l)):
  13. for l1 in output[i*len(output)/len(l):((i+1)*len(output)/len(l))]:
  14. l1.append(l[i])
  15. for x in output:
  16. x.sort()
  17. return output
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,022
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Manipulating a list to generate multiple lists

 
0
  #4
Jul 10th, 2006
girish,

off hand, I wouldn't consider your present solution not brute force. You are asking for a lot to happen and using a dictionary is quite elegant. By the way you need to initiate dictionary d = {}

I had played around with it for a while and also used a dictionary, but got stuck in reassembling the list of lists. I will take a closer look at your solution. If I discover any improvements, I let you know.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC