Understanding a Python list - need elements of missing combinations
For this input list
[0, 1, 2, 3, 4, 5]
I need this 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 this code,
for k in range( 0, 5 ):
for i in range( len( inputlist ) - ( 2 + k ) ):
print [inputlist[k], inputlist[i + ( 2 + k )]]
for i in range( len( inputlist ) - ( 3 + k ) ):
print [inputlist[k], inputlist[i + ( 2 + k )], inputlist[i + ( 3 + k )]]
for i in range( len( inputlist ) - ( 4 + k ) ):
print [inputlist[k], inputlist[i + ( 2 + k )], inputlist[i + ( 3 + k )], inputlist[i + ( 4 + k )]]
I need to skip patterns, 1,2,3 → 1,3 1,2,3,4 → [1,3], [1,4], [2,4]
those. first element, third element, etc.
How to summarize this? Help is appreciated
+3
source to share
2 answers
Try to describe your problem in words.
From what I understand from your example:
def good(x): return x[0]+1!=x[1] and all(i+1==j for i,j in zip(x[1:],x[2:]))
from itertools import combinations
[i for j in range(2,5) for i in filter(good, combinations(l,j))]
<0> (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)]
+5
source to share
from itertools import combinations
a=range(6)
combs=[list(combinations(a,j)) for j in range(2,5)]
combs1=[list(i) for elem in combs for i in elem if (len(i)==2 and i[1] -i[0] >1) or (len(i)>=3 and i[-1]-i[-2] ==1 and i[1] -i[0]>1 and i[2]-i[1] ==1)]
print combs1
#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]]
0
source to share