hi 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:
[['a.1','b.3','c.2'],['a.1','b.4','c.2']]
Another example: Let l = . Then
output should be [,,
,[

Can anyone suggest me a time-efficient (non brute force) method for doing this??

TIA,
girish

What is your present solution, even if brute force?

hi,
Please look at the following:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.