【发布时间】: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