itertools函數沒有錯誤,但它在完成後也不會打印任何內容。 我的代碼是:問題打印組合
def comb(iterable, r):
pool = tuple(iterable)
n = len(pool)
for indices in permutations(range(n), r):
if sorted(indices) == list(indices):
print (indices)
yield tuple(pool[i] for i in indices)
我包括print語句,但它並不打印計算的總組合。
爲什麼要創建一個整數列表,只要輸入數據,產生它們的排列,使用每個排列作爲一對索引到原始併產生結果?噢,因爲你想過濾他們訂購?這就是'itertools.combinations'的用途;它直接取代你的整個功能。 – 2012-04-18 06:30:50