3

複数の辞書に共通の文字列がある場合、複数の辞書を論理的にマージする方法はありますか? これらの一般的な文字列が、ある dict() の値と別のキーの値の間で一致する場合でも?

SOで同様の質問がたくさんありますが、「下位レベルのファイル」の複数のキーを上位のキー/値(level1dict)のキーに関連付けるという私の特定の問題に対処しているようには見えません

私たちが持っているとしましょう:

level1dict = { '1':[1,3], '2':2 }
level2dict = { '1':4, '3':[5,9], '2':10 }
level3dict = { '1':[6,8,11], '4':12, '2':13, '3':[14,15], '5':16, '9':17, '10':[18,19,20]}
finaldict = level1dict

論理的に言うと、level1dict では 1=1,3、level2dict では 1=4 および 3=5,9 なので、全体として (これまでのところ) 1 = 1,3,4,5,9 (ソートは重要ではありません)

私が達成したい結果は

#.update or .append or .default?
finaldict = {'1':[1,3,4,5,9,6,8,11,12,14,15,16,17] '2':[2,10,18,19,20]}

回答済み: networkx モジュールを提供してくれた Ashwini Chaudhary と Abhijit に感謝します。

4

3 に答える 3

9

これは連結成分のサブグラフの問題であり、 networkxを使用する場合に最もよく判断できます。ここにあなたの問題の解決策があります

>>> import networkx as nx
>>> level1dict = { '1':[1,3], '2':2 }
>>> level2dict = { '1':4, '3':[5,9], '2':10 }
>>> level3dict = { '1':[6,8,11], '4':12, '2':13, '3':[14,15], '5':16, '9':17, '10':[18,19,20]}
>>> G=nx.Graph()
>>> for lvl in level:
    for key, value in lvl.items():
        key = int(key)
        try:
            for node in value:
                G.add_edge(key, node)
        except TypeError:
            G.add_edge(key, value)


>>> for sg in nx.connected_component_subgraphs(G):
    print sg.nodes()


[1, 3, 4, 5, 6, 8, 9, 11, 12, 14, 15, 16, 17]
[2, 10, 13, 18, 19, 20]
>>> 

これを視覚化する方法は次のとおりです

>>> import matplotlib.pyplot as plt
>>> nx.draw(G)
>>> plt.show()

ここに画像の説明を入力

于 2012-12-05T08:10:21.083 に答える
2

いくつかのメモ:

  1. 一部の値が数値であり、一部がリストであるのは不便です。最初に数値を1項目リストに変換してみてください。
  2. 順序が重要でない場合はset、リストの代わりにsを使用することをお勧めします。それらには、あらゆる種類の「論理」操作のためのメソッドがあります。

次に、次のことができます。

In [1]: dict1 = {'1': {1, 3}, '2': {2}}

In [2]: dict2 = {'1': {4}, '2': {10}, '3': {5, 9}}

In [3]: dict3 = {'1': {6, 8, 11}, '2': {13}, '4': {12}}

In [4]: {k: set.union(*(d[k] for d in (dict1, dict2, dict3)))
    for k in set.intersection(*(set(d.keys()) for d in (dict1, dict2, dict3)))}
Out[4]: {'1': set([1, 3, 4, 6, 8, 11]), '2': set([2, 10, 13])}
于 2012-12-05T08:02:12.220 に答える
2
In [106]: level1dict = { '1':[1,3], '2':2 }

In [107]: level2dict = { '1':4, '3':[5,9], '2':10 }
In [108]: level3dict = { '1':[6,8,11], '4':12, '2':13, '3':[14,15], '5':16, '9':17, '10':[18,19,20]}

In [109]: keys=set(level2dict) & set(level1dict) & set(level3dict) #returns ['1','2']
In [110]: dic={}

In [111]: for key in keys:
    dic[key]=[]
    for x in (level1dict,level2dict,level3dict):
        if isinstance(x[key],int):
            dic[key].append(x[key])
        elif isinstance(x[key],list):
            dic[key].extend(x[key])
   .....:             

In [112]: dic
Out[112]: {'1': [1, 3, 4, 6, 8, 11], '2': [2, 10, 13]}

# now iterate over `dic` again to get the values related to the items present
# in the keys `'1'` and `'2'`.

In [122]: for x in dic:
    for y in dic[x]:
        for z in (level1dict,level2dict,level3dict):
            if str(y) in z and str(y) not in dic:
                if isinstance(z[str(y)],(int,str)):
                     dic[x].append(z[str(y)])
                elif isinstance(z[str(y)],list):
                     dic[x].extend(z[str(y)])
   .....:                     

In [123]: dic
Out[123]: 
{'1': [1, 3, 4, 6, 8, 11, 5, 9, 14, 15, 12, 16, 17],
 '2': [2, 10, 13, 18, 19, 20]}
于 2012-12-05T08:18:22.410 に答える