| | |
Manipulating a list to generate multiple lists
Thread Solved |
•
•
Join Date: May 2006
Posts: 23
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: May 2006
Posts: 23
Reputation:
Solved Threads: 0
hi,
Please look at the following:
Please look at the following:
Python Syntax (Toggle Plain Text)
def flatten(l): for y in l: l = d.get(y.split('.')[0], []) l.append(y) d[y.split('.')[0]] = l output = [[]] for l in d.values(): for i in range(len(l)-1): for j in output[:]: k = j[:] output.append(k) for i in range(0, len(l)): for l1 in output[i*len(output)/len(l):((i+1)*len(output)/len(l))]: l1.append(l[i]) for x in output: x.sort() return output
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.
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!
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- Does this really generate multiple instances of a UNIX process (Perl)
- generate random question (C)
Other Threads in the Python Forum
- Previous Thread: YAC wxPython Tiny Calculator
- Next Thread: every sentence in a new file
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory drive dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input itunes java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo verify webservice wikipedia write wxpython xlib






