Modify for list output in recursion

江枫思渺然 提交于 2020-01-25 07:05:02

问题


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)

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.


回答1:


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, [])

Output:

[[(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)]]

Output editted for visiblity:

[[(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)]]


来源:https://stackoverflow.com/questions/59901396/modify-for-list-output-in-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!