I have a list in which there is an undefined number of elements:
l1 = [a, b, c, d ...]
I need to create a list such that:
l2 = [[a,a],[a,b],[b,b],[b,c],[c,c],[c,d],[d,d],[d,e],...]
Now, in order to obtain l2, this is what I did:
l1 = sorted(l1*4)[1:-1]
l2 = [l1[x:x+2] for x in xrange(0,len(l1),2)]
It works, but I don't like it because in case the number of elements inside l1 is very big then this code will be quite memory (*4) and time (sorted) consuming. Do you have any tips on how to do this?
Aucun commentaire:
Enregistrer un commentaire