Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法用Python计算一个集合的所有合法分区?_Python_Math_Sympy_Set Theory - Fatal编程技术网

有没有办法用Python计算一个集合的所有合法分区?

有没有办法用Python计算一个集合的所有合法分区?,python,math,sympy,set-theory,Python,Math,Sympy,Set Theory,Wiki给出了一个集合的分区 在数学中,集合的划分是将集合中的元素分组为非空子集,这样每个元素都只包含在一个子集中 这个例子 集合{1,2,3}有这五个分区 { {1}, {2}, {3} }, sometimes written 1|2|3. { {1, 2}, {3} }, or 12|3. { {1, 3}, {2} }, or 13|2. { {1}, {2, 3} }, or 1|23. { {1, 2, 3} }, or 123 有没有办法用Python计算一个集合的所有合法分区

Wiki给出了一个集合的分区

在数学中,集合的划分是将集合中的元素分组为非空子集,这样每个元素都只包含在一个子集中

这个例子

集合{1,2,3}有这五个分区

{ {1}, {2}, {3} }, sometimes written 1|2|3.
{ {1, 2}, {3} }, or 12|3.
{ {1, 3}, {2} }, or 13|2.
{ {1}, {2, 3} }, or 1|23.
{ {1, 2, 3} }, or 123
有没有办法用Python计算一个集合的所有合法分区

我试过了

我得到了

(1, 2, 3)

这显然是错误的。

如果您使用的是Symphy,那么您需要:

>>从sympy.utilities.iterables导入多集分区
>>>对于多集_分区([1,2,3])中的p:
...     印刷品(p)
...
[[1, 2, 3]]
[[1, 2], [3]]
[[1, 3], [2]]
[[1], [2, 3]]
[[1], [2], [3]]
(1, 2, 3)