optimization - Python, combinations, permutations without repeat -


python. have 2 lists, same length. idea build paired data (for regression analysis). figured out loops , this.

a=(1,3,5,7)   #first list b=(2,4,6,10)  #second list w=zip(a,b)    #paired values both lists  i=0 j=0 each in w:     x= w[i]     in xrange(i,len(w)-1):         i+=1         print x, w[i]     j+=1     i=j 

the output expected - first pair second, third.. on, second pair third, fourth.. , on (skipping combination between second pair , first pair, because kinda same combination of first , second pairs...)

(1, 2) (3, 4) (1, 2) (5, 6) (1, 2) (7, 10) (3, 4) (5, 6) (3, 4) (7, 10) (5, 6) (7, 10) [..] , on expecting. 

the question - there other, shorter, optimized ways how rewrite code, maybe using itertools?

yes: itertools.combinations

print list(itertools.combinations(w, 2)) 

you mention in question - i'm not sure why wouldn't in docs before asking on stackoverflow.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -