Python 列表理解 - 需要元素跳过组合

Python list comprehension - need elements skipped combinations

对于这个输入列表

[0, 1, 2, 3, 4, 5]

我需要这个输出

[[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]]

我试过这个代码,

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 )]]

我需要跳过的模式, 1,2,3 --> 1,3 1,2,3,4 --> [1,3],[1,4],[2,4]

即第一个元素,第三个元素等等。

如何概括这一点?感谢帮助

尝试用文字描述您的问题。

从你的例子中我了解到:

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, 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)]

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]]