兩件事情在這裏錯了:
首先
return set(main_list)
使用:return main_list
來代替。
對於設置哪個需要可排列類型,哪個列表不可以,因爲列表是可變的。你也可以在這裏使用一個元組,這是不可變的。
另一個
您不能追加,並在同一時間main_list的引用傳遞到遞歸。因此,像:
main_list.append(sublist)
return subsets(setys, count + 1, main_list)
會爲你工作。
輸出:
[]
[[0]]
[[0], [0, 1], [0, 1]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99]]
[[0], [0, 1], [0, 1], [0, 1, 34], [0, 1, 34], [0, 1, 34], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99], [0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99]]
編輯
添加到您的return語句:
final_set = set()
for item in tuple(main_list):
final_set.add(tuple(item))
return list(final_set)
最終輸出:
[(0, 1), (0,), (0, 1, 34, 3, 5, 6, 7, 9, 2, None, 99), (0, 1, 34), (0, 1, 34, 3, 5, 6, 7, 9, 2), (0, 1, 34, 3, 5), (0, 1, 34, 3, 5, 6, 7), (0, 1, 34, 3, 5, 6, 7, 9), (0, 1, 34, 3, 5, 6), (0, 1, 34, 3, 5, 6, 7, 9, 2, None), (0, 1, 34, 3)]
'main_list.append(sublist)'是'None',所以你實際上將'None'傳遞給'subsets'而不是修改'main_list'。並使用[可變的默認參數是一個壞主意](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument)。 –
當我向'sublist'追加'set_to_list'的第一個值時,'main_list.append(suiblist)'是'None'怎麼辦? – Liondancer