2

我想从下面给定的列表中找到匹配的项目。我的列表可能超级大。

元组“N1_10”中的第一个项目被复制并与另一个数组中的另一个项目匹配

ListA 中第一个数组中的元组 ListA('N1_10', 'N2_28')
中第二个数组中的元组('N1_10', 'N3_98')

ListA  = [[('N1_10', 'N2_28'), ('N1_35', 'N2_44')],
          [('N1_22', 'N3_72'), ('N1_10', 'N3_98')],
          [('N2_33', 'N3_28'), ('N2_55', 'N3_62'), ('N2_61', 'N3_37')]]

我想要的输出是

输出 -->[('N1_10','N2_28','N3_98') , ....其余任何匹配的键之一都将进入同一个元组]

如果你们认为,改变 ListA 的数据结构是更好的选择,请随时提出建议!感谢您的帮助!

简化版

列表 A = [[( a,x ),(b,k),(c,l),( d,m )],[( e,d ),( a,p ),(g,s)], [...],[...]....]

想要的输出 --> [( a,x,p ),(b,k),(c,l),( d,m,e ),(g,s).....]

4

3 回答 3

3

更新:重新阅读您的问题后,您似乎正在尝试创建等价类,而不是收集键的值。如果

[[(1, 2), (3, 4), (2, 3)]]

应该成为

[(1, 2, 3, 4)]

,那么您将需要将输入解释为图形并应用连通分量算法。您可以将您的数据结构转换为邻接列表表示,并使用广度优先或深度优先搜索来遍历它,或者遍历您的列表并构建不相交的集合。无论哪种情况,您的代码都会突然涉及大量与图形相关的复杂性,并且很难根据输入的顺序提供任何输出顺序保证。这是一个基于广度优先搜索的算法:

import collections

# build an adjacency list representation of your input
graph = collections.defaultdict(set)
for l in ListA:
    for first, second in l:
        graph[first].add(second)
        graph[second].add(first)

# breadth-first search the graph to produce the output
output = []
marked = set() # a set of all nodes whose connected component is known
for node in graph:
    if node not in marked:
        # this node is not in any previously seen connected component
        # run a breadth-first search to determine its connected component
        frontier = set([node])
        connected_component = []
        while frontier:
            marked |= frontier
            connected_component.extend(frontier)

            # find all unmarked nodes directly connected to frontier nodes
            # they will form the new frontier
            new_frontier = set()
            for node in frontier:
                new_frontier |= graph[node] - marked
            frontier = new_frontier
        output.append(tuple(connected_component))

但是,不要只是在不理解的情况下复制它;了解它在做什么,或者编写自己的实现。您可能需要能够维护这一点。(我会使用伪代码,但 Python 实际上已经和伪代码一样简单了。)

如果我对您的问题的原始解释是正确的,并且您的输入是您想要聚合的键值对的集合,那么这是我的原始答案:

原始答案

import collections

clusterer = collections.defaultdict(list)

for l in ListA:
    for k, v in l:
        clusterer[k].append(v)

output = clusterer.values()

defaultdict(list)是 a dict,它会自动创建 alist作为任何不存在的键的值。循环遍历所有元组,收集与同一键匹配的所有值,然后从 defaultdict 创建 (key, value_list) 对的列表。

(这段代码的输出和你指定的形式不太一样,但我相信这个形式更有用。如果你想改变形式,那应该是一件简单的事情。)

于 2013-07-05T07:49:48.087 回答
2
tupleList = [(1, 2), (3, 4), (1, 4), (3, 2), (1, 2), (7, 9), (9, 8), (5, 6)]

newSetSet = set ([frozenset (aTuple) for aTuple in tupleList])
setSet = set ()

while newSetSet != setSet:
    print '*'
    setSet = newSetSet
    newSetSet = set ()
    for set0 in setSet:
        merged = False
        for set1 in setSet:
            if set0 & set1 and set0 != set1:
                newSetSet.add (set0 | set1)
                merged = True
        if not merged:
            newSetSet.add (set0)

        print [tuple (element) for element in setSet]
        print [tuple (element) for element in newSetSet]
        print 

print [tuple (element) for element in newSetSet]

# Result:  [(1, 2, 3, 4), (5, 6), (8, 9, 7)]
于 2013-07-05T07:56:48.987 回答
2

输出顺序重要吗?这是我能想到的最简单的方法:

ListA  = [[('N1_10', 'N2_28'), ('N1_35', 'N2_44')],[('N1_22', 'N3_72'), ('N1_10', 'N3_98')],
            [('N2_33', 'N3_28'), ('N2_55', 'N3_62'), ('N2_61', 'N3_37')]]

idx = dict()

for sublist in ListA:
    for pair in sublist:
        for item in pair:
            mapping = idx.get(item,set())
            mapping.update(pair)
            idx[item] = mapping 
            for subitem in mapping:
                submapping = idx.get(subitem,set())
                submapping.update(mapping)
                idx[subitem] = submapping


for x in set([frozenset(x) for x in idx.values()]):
    print list(x)

输出:

['N3_72', 'N1_22']
['N2_28', 'N3_98', 'N1_10']
['N2_61', 'N3_37']
['N2_33', 'N3_28']
['N2_55', 'N3_62']
['N2_44', 'N1_35']
于 2013-07-05T08:07:38.343 回答