从列表中的字符串对中收集关系

Collecting relationships from pairs of strings in lists

考虑下面的一组列表,每个列表包含两个字符串。

给定列表中的两个字符串配对意味着它们表示的值相等。所以 A 项与 B 项和 C 项相同,依此类推。

l1 = [ 'A' , 'B' ] 
l2 = [ 'A' , 'C' ]
l3 = [ 'B' , 'C' ] 

什么是最 efficient/pythonic 将这些关系收集到字典中的方法,如下所示:

{ "A" : [ "B" , "C" ] } 

p.s。不好意思的标题,我不知道如何描述这个问题!

编辑:

为了让问题更清楚,我正在尝试从数千条记录中过滤掉重复的样本。

我对数据集中的每个样本进行了成对比较,表明它们是否相互重复。

有时,样本可能会以不同的标识符出现在 triplicate/quadruplicate 的数据集中。

只保留一个重复样本很重要。

因此需要一个字典或类似的结构,其中包含所选样本作为键,并将其在数据集中的重复项列表作为值。

我会使用集合中的 defaultdict,这样字典就可以扩展并只接受你放入其中的任何内容。如果这是单向通信,我会使用此代码(假设您列出了列表)

dd = collections.defaultdict(list)

for line in lists:
    dd[line[0]].append(line[1])

对于给定的集合,这将生成字典

{"A" : ["B", "C"] }
{"B" : ["C"] }
'''
this should be a good starting point

假设您可以像这样获得这些列表的列表:

[[ 'A' , 'B' ], [ 'A' , 'C' ],[ 'B' , 'C' ]]

这应该给你你想要的关系:

d = {}

l1 = [[ 'A' , 'B' ],[ 'A' , 'C' ],[ 'B' , 'C' ]] 

for sublist in l1:
    if sublist[0] not in d.keys(): #check if first value is already a key in the dict
        d[sublist[0]] = [] #init new key with empty list
    d[sublist[0]].append(sublist[1]) #append second value

输出:

{'A': ['B', 'C'], 'B': ['C']}

我有一个相当冗长的答案,如果它已经被添加到字典中,它会存储一个 ID。

但一定有更有效的答案!

l = [["A", "B"], ["A", "C"], ["B", "C"]]

d = dict()
used_list = list()

for i in l:
    id_one, id_two = i

    if not id_one in used_list and id_one not in d.keys():

        d[id_one] = [id_two]
        used_list.append(id_two)
        continue

    if not id_one in used_list and id_one in d.keys():
        d[id_one].append(id_two)
        used_list.append(id_two)

print( d ) 

Returns:

{'A': ['B', 'C']}