import itertools
s="abcde"
def upto_n(s,n):
out = []
for i in range(1,n+1,1):
out += list(itertools.combinations(s, i))
return out
print upto_n(s,2)
print upto_n(s,3)
輸出
[('a',), ('b',), ('c',), ('d',), ('e',), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e'), ('c', 'd'), ('c', 'e'), ('d', 'e')]
[('a',), ('b',), ('c',), ('d',), ('e',), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e'), ('c', 'd'), ('c', 'e'), ('d', 'e'), ('a', 'b', 'c'), ('a', 'b', 'd'), ('a', 'b', 'e'), ('a', 'c', 'd'), ('a', 'c', 'e'), ('a', 'd', 'e'), ('b', 'c', 'd'), ('b', 'c', 'e'), ('b', 'd', 'e'), ('c', 'd', 'e')]
分享您的代碼,請。看起來你只需要產生長度爲1,然後是2,然後是3等的'排列'......直到你想要的長度,這可以通過'for'循環容易地完成。 – Julien
這不是排列!告訴我們你的代碼和預期的輸出與實際輸出 – alfasin