我正在尝试编写一个递归函数,该函数将在元组列表中查找匹配项,并将它们分组。示例列表可以是:
l = [(1,2),(2,3),(4,6),(3,5),(7,9),(6,8)]
目的是以以下形式结束三个子列表:
l = [(1,2),(2,3),(3,5)],[(4,6),(6,8)],[7,9]]
这似乎是递归函数的理想情况,但我还没有使它工作。这是我到目前为止所写的内容:
count = 0
def network(index_list, tuples_list, groups, count):
if len(index_list) > 0:
i = 0
for j in range (len(index_list)):
match = set(tuples_list[groups[count][i]]) & set(tuples_list[index_list[j]])
if len(match) > 0:
groups[count].append(index_list[j])
index_list.pop(j)
i += 1
else:
count += 1
groups.append([])
groups[count].append(index_list[0])
network(index_list, tuples_list, groups, count)
else:
return groups
此外,我很确定这个问题与标记重复的问题不同。我正在寻找一个递归解决方案,它可以保持所有元组的完整性,在这种情况下,通过append和pop推送它们的索引。我很肯定这是一个优雅的递归解决方案。
答案 0 :(得分:0)
我不确定递归在这里是否有用。这是一个有趣的思想实验,所以我试了一下,但是我的递归函数只需要一次运行。
根据评论进行修改,仅匹配元组的前两个数据点
import copy
#Inital 1-item groups
def groupTuples(tupleList):
groups = []
for t in tupleList:
groups.append([t])
return groups
#Merge two groups
def splice(list1, list2, index):
for item in list2:
list1.insert(index, item)
index += 1
return list1
def network(groups = []):
#Groups get modified by reference, need a shallow copy for reference
oldGroups = copy.copy(groups)
for group in groups:
for index, secondGroup in enumerate(groups):
if group == secondGroup:
#Splice can get caught in a loop if passed the same item twice.
continue
#Last item of First Group matches First item of Second Group
if group[-1][1] == secondGroup[0][0]:
splice(group, secondGroup, len(group))
groups.pop(index)
#First item of First Group matches Last item of Second Group
elif group[0][0] == secondGroup[-1][1]:
splice(group, secondGroup, 0)
groups.pop(index)
if groups == oldGroups:
return groups #No change.
else:
return network(groups)
l = [(1,2,0),(2,3,1),(4,6,2),(3,5,3),(1,7,4),(7,9,5),(6,8,6),(12, 13,7),(5, 12,8)]
result = network(groupTuples(l))
print result
结果:[[(1, 2, 0), (2, 3, 1), (3, 5, 3), (5, 12, 8), (12, 13, 7)], [(1, 7, 4), (7, 9, 5)], [(4, 6, 2), (6, 8, 6)]]
它确实在数据集上多次运行匹配算法,但是我还没有看到第二次迭代产生任何变化的数据配置,所以不确定这里是否需要递归。