【问题标题】:Modify for list output in recursion以递归方式修改列表输出
【发布时间】:2020-01-24 17:47:56
【问题描述】:

Find all possible combinations that overlap by end and start 中,我们得到以下程序,该程序获取仅在结束值和开始值重叠的范围的所有可能组合。输出以字符串格式给出,如下例所示:

def getAllEndOverlappingIndices(lst, i, l):
    r = -1
    if i == len(lst):
        if l:
            print(l)
        return

    n = i + 1
    while n < len(lst) and r > lst[n][0]:
        n += 1
    getAllEndOverlappingIndices(lst, n, l)

    n = i + 1
    r = lst[i][1]
    while n < len(lst) and r > lst[n][0]:
        n += 1
    getAllEndOverlappingIndices(lst, n, l + str(lst[i]))

indices = [(0.0, 2.0), (0.0, 4.0), (2.5, 4.5), (2.0, 5.75), (2.0, 4.0), (6.0, 7.25)]
indices.sort()

print(getAllEndOverlappingIndices(indices, 0, ''))
(6.0, 7.25)
(2.5, 4.5)
(2.5, 4.5)(6.0, 7.25)
(2.0, 5.75)
(2.0, 5.75)(6.0, 7.25)
(2.0, 4.0)
(2.0, 4.0)(6.0, 7.25)
(0.0, 4.0)
(0.0, 4.0)(6.0, 7.25)
(0.0, 2.0)
(0.0, 2.0)(6.0, 7.25)
(0.0, 2.0)(2.5, 4.5)
(0.0, 2.0)(2.5, 4.5)(6.0, 7.25)
(0.0, 2.0)(2.0, 5.75)
(0.0, 2.0)(2.0, 5.75)(6.0, 7.25)
(0.0, 2.0)(2.0, 4.0)
(0.0, 2.0)(2.0, 4.0)(6.0, 7.25)

我希望函数的输出是一个列表列表,每个子列表都包含 分隔的 元组(目前,正在返回元组的字符串)。对于上面给出的示例,输出将改为L = [[(6.0, 7.25)], [(2.5, 4.5)], [(2.5, 4.5), (6.0, 7.25)], ..., [(0.0, 2.0), (2.0, 4.0), (6.0, 7.25)]]。我尝试在函数的开头创建一个列表并附加函数的每个调用,但我相信我正在附加元组的字符串而不是元组本身。是否可以将此函数的输出更改为我想要的描述?我在递归方面没有太多经验,希望有任何提示。

【问题讨论】:

    标签: python list recursion output


    【解决方案1】:

    现在,代码所做的是创建一个巨大的字符串,因为您从一个字符串开始并将每个元组转换为一个字符串,然后再将所有内容连接在一起。相反,您可以做的是传入一个空列表并将元组添加到列表中,直到您完成组合。到达组合的末尾后,将其添加到包含所有组合的全局数组中。

    # Create a global array to hold all your results.
    results = []
    
    def getAllEndOverlappingIndices(lst, i, l):
        r = -1
        if i == len(lst):
            if l:
                # Instead of printing final combination, add the combination to the global list
                results.append(l)
            return
    
        n = i + 1
        while n < len(lst) and r > lst[n][0]:
            n += 1
        getAllEndOverlappingIndices(lst, n, l)
    
        n = i + 1
        r = lst[i][1]
        while n < len(lst) and r > lst[n][0]:
            n += 1
        # Wrap the tuple in the list to take advantage of python's list concatenation
        getAllEndOverlappingIndices(lst, n, l + [lst[i]])
    
    indices = [(0.0, 2.0), (0.0, 4.0), (2.5, 4.5), (2.0, 5.75), (2.0, 4.0), (6.0, 7.25)]
    indices.sort()
    # Pass in an empty list here instead of an empty string
    getAllEndOverlappingIndices(indices, 0, [])
    

    输出:

    [[(6.0, 7.25)], [(2.5, 4.5)], [(2.5, 4.5), (6.0, 7.25)], [(2.0, 5.75)], [(2.0, 5.75), (6.0, 7.25)], [(2.0, 4.0)], [(2.0, 4.0), (6.0, 7.25)], [(0.0, 4.0)], [(0.0, 4.0), (6.0, 7.25)], [(0.0, 2.0)], [(0.0, 2.0), (6.0, 7.25)], [(0.0, 2.0), (2.5, 4.5)], [(0.0, 2.0), (2.5, 4.5), (6.0, 7.25)], [(0.0, 2.0), (2.0, 5.75)], [(0.0, 2.0), (2.0, 5.75), (6.0, 7.25)], [(0.0, 2.0), (2.0, 4.0)], [(0.0, 2.0), (2.0, 4.0), (6.0, 7.25)]]
    

    为可见性编辑输出:

    [[(6.0, 7.25)],
    [(2.5, 4.5)],
    [(2.5, 4.5), (6.0, 7.25)],
    [(2.0, 5.75)],
    [(2.0, 5.75), (6.0, 7.25)],
    [(2.0, 4.0)],
    [(2.0, 4.0), (6.0, 7.25)],
    [(0.0, 4.0)],
    [(0.0, 4.0), (6.0, 7.25)],
    [(0.0, 2.0)],
    [(0.0, 2.0), (6.0, 7.25)],
    [(0.0, 2.0), (2.5, 4.5)],
    [(0.0, 2.0), (2.5, 4.5), (6.0, 7.25)],
    [(0.0, 2.0), (2.0, 5.75)],
    [(0.0, 2.0), (2.0, 5.75), (6.0, 7.25)],
    [(0.0, 2.0), (2.0, 4.0)],
    [(0.0, 2.0), (2.0, 4.0), (6.0, 7.25)]]
    

    【讨论】:

    • 嘿,@Lapis Rose。感谢您对上述问题的帮助!正如您在stackoverflow.com/questions/62734114/… 看到的那样,我正在尝试获得一个生成相同输出的迭代解决方案,但我遇到了麻烦。您对如何做到这一点有任何想法吗?
    猜你喜欢
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多