使用妳的問題的方法是組合([1,2,3,...,n],m)。
定義組合(lst,k):
is _ in =[1]* k+[0]*(len(lst)-k)
top_begin,top_end = 0,len(lst)-k
L = len(lst)
while top_begin!= top_end:
_combination = tuple([x for x,y in zip(lst,is_in) if y == 1])
產量_組合
對於範圍內的I(L-1,0,-1):
如果is_in[i-1] == 1且is_in[i] == 0:
leftOnes = sum(is_in[i :])
is _ in = is _ in[:I-1]+[0]+[1]*(left ones+1)+[0]*(L-I-left ones-1)
top _ begin+= int(is _ in[top _ begin]!= 1)
破裂
yield tuple(lst[-k :])