修改递归列表输出 [英] Modify for list output in recursion

查看:63
本文介绍了修改递归列表输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内找到所有可能重叠的组合并开始,我们将获得以下程序,该程序将获得仅在结束值和起始值重叠的范围的所有可能组合.输出以字符串格式给出,如以下示例所示:

In Find all possible combinations that overlap by end and start, we get the following program that gets all possible combinations of ranges that only overlap by end and start values. The output is given in string format, as with the following example:

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)]].我尝试在函数的开头创建一个列表,并追加函数的每个调用,但是我相信我要追加元组的字符串,而不是元组本身.是否可以将此功能的输出更改为我想要的描述?我没有太多的递归经验,不胜感激任何技巧.

I would like to have the output of the function be a list of lists with each sublist holding the separated tuples (currently, strings of the tuples are being returned). For the example given above, the output would instead be 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)]]. I tried creating a list at beginning of the function and appending each call of the function, but I believe I am appending strings of the tuples instead of the tuples themselves. Is it possible to change the output of this function to what I would like as described? I don't have much experience with recursion and would appreciate any tips.

推荐答案

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

Right now, what the code does is create a giant string because you start with a string and cast every tuple into a string before concatenating everything together. Instead, what you can do is pass in an empty list and add tuples to the list until you finish your combination. Once you get to the end of the combination, add it to a global array that holds all your combinations.

# 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)]]

这篇关于修改递归列表输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆