2017-07-11 143 views
2

我使用itertools得到一個列表的所有組合:蟒蛇限制itertools導致

import itertools 

stuff = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
for L in range(0, len(stuff)+1): 
    for subset in itertools.combinations(stuff, L): 
    print(subset) 

有沒有辦法來限制結果如至少有2個和最多5個元素的組合?

謝謝!

+1

排列和組合是兩個不同的事情。 – chepner

+0

好的。 ...我會編輯帖子@chepner – Ele

回答

4

只要改變for循環。

import itertools 

stuff = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
for L in range(2, 6): 
    for subset in itertools.combinations(stuff, L): 
    print(subset) 
+0

很快。謝謝! – Ele

8

限制的範圍內,以[2, 6)

for L in range(2, 6): 
    for subset in itertools.combinations(stuff, L): 
     print(subset) 
+0

這很快。謝謝! – Ele