我正在尝试查找集合的所有子集并将其打印到列表中。 但是,我目前遇到此错误。
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "subset_of_set.py", line 15, in <module>
print subsets(a)
File "subset_of_set.py", line 9, in subsets
return subsets(setys, count + 1, main_list.append(sublist))
File "subset_of_set.py", line 9, in subsets
return subsets(setys, count + 1, main_list.append(sublist))
AttributeError: 'NoneType' object has no attribute 'append'
但是当我在解释器上测试.append
时,似乎很好。
>>> f = []
>>> f.append(3)
>>> f
[3]
我可能缺少或不知道。有人可以解释一下吗?谢谢!
我的代码:
def subsets(setys, count = 1,main_list = []):
set_to_list = list(setys)
length_of_set = len(setys)
sublist = []
if count == length_of_set:
return set(main_list)
for n in set_to_list[:count]:
sublist.append(n)
return subsets(setys, count + 1, main_list.append(sublist))
a = [3,5,3,2,5,7,34,2,54,6,7,1,1,1,0,None,3,99,9]
a = set(a)
print subsets(a)
答案 0 :(得分:2)
return subsets(setys, count + 1, main_list.append(sublist))
确实是您的错误所在。考虑将其更改为
main_list.append(sublist)
return subsets(setys, count + 1, main_list)
答案 1 :(得分:2)
这里有两件事是错的:
<强>第一强>
return set(main_list)
使用:return main_list
代替。
For,Set需要一个hashable类型,哪个列表不是,因为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)]