Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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 - Fatal编程技术网

Python 重建此生成器以生成嵌套列表的排序嵌套元组

Python 重建此生成器以生成嵌套列表的排序嵌套元组,python,Python,我目前有一个生成嵌套列表的folliwng生成器(用于构造一个集合的分区集),但我实际上需要它来输出嵌套元组。我目前使用的是一个to_tuple hack,但是你能帮我在生成器中实现它吗 def _partition(collection): #from here : https://stackoverflow.com/questions/19368375/set-partitions-in-python/61141601" if len(collection) ==

我目前有一个生成嵌套列表的folliwng生成器(用于构造一个集合的分区集),但我实际上需要它来输出嵌套元组。我目前使用的是一个to_tuple hack,但是你能帮我在生成器中实现它吗

def _partition(collection):
    #from here : https://stackoverflow.com/questions/19368375/set-partitions-in-python/61141601"
    if len(collection) == 1:
        yield [ collection ]
        return
    first = collection[0]
    for smaller in _partition(collection[1:]):
        # insert `first` in each of the subpartition's subsets
        for n, subset in enumerate(smaller):
            yield smaller[:n] + [[ first ] + subset]  + smaller[n+1:]
        # put `first` in its own subset
        yield [ [ first ] ] + smaller

def to_tuple(lst):
    return tuple(to_tuple(i) if isinstance(i, list) else i for i in lst)


## using it 
exemple = [0,0,1,4]
pp = [sorted(p) for p in _partition(exemple)]
result = to_tuple(pp)
以下是我得到的:

def_分区(集合):
#从这里开始:https://stackoverflow.com/questions/19368375/set-partitions-in-python/61141601"
集合=元组(集合)
如果len(集合)==1:
收益率(集合)
返回
第一个=集合[0]
对于较小的in_分区(集合[1:]):
#在每个子分区的子集中插入“first”
对于n,枚举中的子集(较小):
产出元组(已排序)(
更小的,更小的
+(元组(排序((第一,)+子集)),)
+更小的[n+1:]
))
#将'first'放在它自己的子集中
产量元组(排序(((第一,),)+更小))

example=[0,0,1,4]
对于p in_分区(示例):
印刷品(p)
产出将是:

((0,0,1,4),)
((0,), (0, 1, 4))
((0, 0), (1, 4))
((0,), (0, 1, 4))
((0,), (0,), (1, 4))
((0, 0, 1), (4,))
((0, 1), (0, 4))
((0,), (0, 1), (4,))
((0, 0, 4), (1,))
((0, 1), (0, 4))
((0,), (0, 4), (1,))
((0, 0), (1,), (4,))
((0,), (0, 1), (4,))
((0,), (0, 4), (1,))
((0,), (0,), (1,), (4,))
在未分类的情况下

example=[2,0,1,4]
对于p in_分区(示例):
印刷品(p)
它输出

((0,1,2,4),)
((0, 1, 4), (2,))
((0, 2), (1, 4))
((0,), (1, 2, 4))
((0,), (1, 4), (2,))
((0, 1, 2), (4,))
((0, 1), (2, 4))
((0, 1), (2,), (4,))
((0, 2, 4), (1,))
((0, 4), (1, 2))
((0, 4), (1,), (2,))
((0, 2), (1,), (4,))
((0,), (1, 2), (4,))
((0,), (1,), (2, 4))
((0,), (1,), (2,), (4,))
最后,通过重复元素:

example=[1,2,2]
对于p in_分区(示例):
印刷品(p)
输出:

((1,2,2),)
((1,), (2, 2))
((1, 2), (2,))
((1, 2), (2,))
((1,), (2,), (2,))
要删除重复项,可以运行
set(_partition(示例))


对于外部级别的排序,我不认为可以使用生成器进行排序,因为我们将逐个获取元素,并且在生成器耗尽之前无法比较它们。

更好。结果在每个分区的每个部分内进行排序,但分区本身不进行排序。例如
示例=[1,2,2]
我获得了两个输出
((1,2),(2,)
((2,),(1,2))
,我希望它们是一样的。是否也可以进行一级排序?(我不是指在输出级进行排序,而是仅复制我放弃的示例的输出(在生成器之后进行排序)。但也许这不可行,你是对的。我仍然对你刚才所做的表示赞赏!玩得好。好的,我添加了另一个级别的排序。现在要删除重复项,你只需运行
set(_partition(示例))
。另一个选择是跟踪所看到的分区,但我不确定如何用递归实现。现在它很完美。实际上,我想使用Counter()而不是set()。我没有在这些计数中找到正确的数学逻辑,但如果我设法找到一个,可能会快得多。是的,计算多个集合分区是相当复杂的数学