[英]How do I make a powerset from an arbitrary set in python?
I had a lab today about making a power set from a set of arbitrary size, but I couldn't seem to think of the best way to do it. 今天,我有一个实验室,准备从任意大小的电源制造电源组,但是我似乎没有想到最佳的制作方法。 No one in the class has done Python before either.
班上没有人做过Python。 Only done java
只做java
The insight is that for each set S in the powerset, a given member of the original set is either in S or it isn't. 洞察力是,对于幂集中的每个集合S,原始集合的给定成员要么在S中,要么不在。 Since this is a binary decision, the powerset doubles for each additional element in the original set.
由于这是一个二进制决策,因此对于原始集中的每个其他元素,powerset都会加倍。
powerset({a}) = {{}, {a}}
powerset({a,b}) = {{}, {a}, {b}, {a,b}}
powerset({a,b,c}) = union(
powerset({a,b}),
union({c}, X) where X in powerset({a,b})
)
This leads to a nice recursive algorithm that works for sets of "arbitrary size" as long as arbitrary is not too large. 这就导致了一种不错的递归算法,只要任意值不太大,该算法就可用于“任意大小”集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.