Generating multiple lists from one list

Amit Khemka khemkaamit at gmail.com
Thu Jul 6 03:31:07 EDT 2006


> On 7/6/06, Girish Sahani <girish at cse.iitb.ac.in> wrote:
>  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??

-----------------------------------------------------------------------------
x = ['a.1','b.3','b.4','c.2','c.6','d.3']
d = {}
for y in x:
        l = d.get(y.split('.')[0], [])
        l.append(y)
        d[y.split('.')[0]] = l

output = [[]]

for l in d.values():
        dfdf = output[:]
        ## ugly hack to work around references +
        for i in range(len(l)-1):
                for j in dfdf:
                        k = j[:]
                        output.append(k)
        # hack -
        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])

print output

--------------------------------------------------------------------------------------------------------------------

hope that helps !

cheers,
amit.


> 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
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
----
Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.



More information about the Python-list mailing list