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:
Another example: Let l = . Then
output should be [,,

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


Recommended Answers

All 3 Replies

What is your present solution, even if brute force?

Please look at the following:

def flatten(l):
    for y in l:
        l = d.get(y.split('.')[0], [])
        d[y.split('.')[0]] = l        
        output = [[]]    
    for l in d.values():
        for i in range(len(l)-1):
            for j in output[:]:
                k = j[:]
        for i in range(0, len(l)):
            for l1 in output[i*len(output)/len(l):((i+1)*len(output)/len(l))]:
    for x in output:
    return output


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.