Python list comprehension - need elements skipped combinations -
for input list
[0, 1, 2, 3, 4, 5]
i need output
[[0, 2], [0, 3], [0, 4], [0, 5], [1, 3], [1, 4], [1, 5], [2, 4], [2, 5], [3, 5], [0, 2, 3], [0, 3, 4], [0, 4, 5], [1, 3, 4], [1, 4, 5], [2, 4, 5], [0, 2, 3, 4], [0, 3, 4, 5], [1, 3, 4, 5]]
i have tried code,
for k in range( 0, 5 ): in range( len( inputlist ) - ( 2 + k ) ): print [inputlist[k], inputlist[i + ( 2 + k )]] in range( len( inputlist ) - ( 3 + k ) ): print [inputlist[k], inputlist[i + ( 2 + k )], inputlist[i + ( 3 + k )]] in range( len( inputlist ) - ( 4 + k ) ): print [inputlist[k], inputlist[i + ( 2 + k )], inputlist[i + ( 3 + k )], inputlist[i + ( 4 + k )]]
i need skipped patterns, 1,2,3 --> 1,3 1,2,3,4 --> [1,3],[1,4],[2,4]
ie, first element, third element , on.
how generalize this? appreciated
try describe words problem.
from understand example:
def good(x): return x[0]+1!=x[1] , all(i+1==j i,j in zip(x[1:],x[2:])) itertools import combinations [i j in range(2,5) in filter(good, combinations(l,j))]
[(0, 2), (0, 3), (0, 4), (0, 5), (1, 3), (1, 4), (1, 5), (2, 4), (2, 5), (3, 5), (0, 2, 3), (0, 3, 4), (0, 4, 5), (1, 3, 4), (1, 4, 5), (2, 4, 5), (0, 2, 3, 4), (0, 3, 4, 5), (1, 3, 4, 5)]
Comments
Post a Comment