我从集合库中获得了一个默认字典,该字典呈现了图的每个节点的所有连接的列表:
edges = {1 : [987, 682, 465],
2 : [45, 67, 85, 907, ...],
...
n : [32, 563, 659, 902]}
我还有另一个,它为每个节点存储它所属的类别,如下所示:
categories = {1 : ['category1', 'category37'],
2 : ['category45', 'category86', ...],
... ,
n : ['category1','category2'], ....}
我想获得一个最终的字典,该字典将为每个类别延续该类别所有连接的字典,如下所示:
final = {'category1' : {1 : [987, 682, 465], 37: [84, 777, 90, 744, 343], ...},
'category2' : {37 : [84, 777, 90, 744, 343], 64: [32, 1222], ...}
...}
答案 0 :(得分:0)
您可以使用嵌套循环来实现。
单行版本:
final = {category: {edge: edges[edge]} for edge in edges \
for category in categories[edge] if category in categories[edge]}
带有明确步骤的详细版本:
final = {}
for edge in edges: # loop through each edge
# find all categories that belong to the current edge in outer loop and then append
# the edge list to the corresponding category in the final dictionary
for category in categories[edge]:
if category in final:
final[category][edge] = edges[edge]
else:
final[category] = {edge: edges[edge]}
为了测试这一点,我举了一个例子。
edges = {1:[11,56,3], 2:[69,4,5,6], 3:[1,8,96,5]}
categories = {1:['category1', 'category37'],2:['category45', 'category86'],3:['category1','category2']}
结果是
{'category1': {1: [11, 56, 3], 3: [1, 8, 96, 5]},
'category37': {1: [11, 56, 3]},
'category45': {2: [69, 4, 5, 6]},
'category86': {2: [69, 4, 5, 6]},
'category2': {3: [1, 8, 96, 5]}}
答案 1 :(得分:0)
任务可以完成如下:
categoryX
,值是categoryX
下的边的列表categoryX
和相应的值是
属于categoryX
我们使用内置的defaultdict来简化中间字典的创建
from collections import defaultdict
edges = {1 : [987, 682, 465],
2 : [45, 67, 85, 907],
3 : [32, 563, 659, 902]}
categories = {1 : ['category1', 'category37'],
2 : ['category45', 'category86'],
3 : ['category1', 'category2']}
intermediate = defaultdict(list)
for k, v in categories.items():
for c in v:
intermediate[c].append(k)
final = defaultdict(dict)
for k, v in intermediate.items():
final[k] = {i:edges[i] for i in v}
我们还可以如下创建列表理解的final
:
last = {k: {i : edges[i] for i in v} for k, v in intermediate.items()}
样品运行:
>>> from pprint import pprint
>>> pprint(final)
defaultdict(<class 'dict'>,
{'category1': {1: [987, 682, 465], 3: [32, 563, 659, 902]},
'category2': {3: [32, 563, 659, 902]},
'category37': {1: [987, 682, 465]},
'category45': {2: [45, 67, 85, 907]},
'category86': {2: [45, 67, 85, 907]}})
>>> pprint(last)
{'category1': {1: [987, 682, 465], 3: [32, 563, 659, 902]},
'category2': {3: [32, 563, 659, 902]},
'category37': {1: [987, 682, 465]},
'category45': {2: [45, 67, 85, 907]},
'category86': {2: [45, 67, 85, 907]}}
使用中间表是一种很好的方式,可以灵活地搜索复杂的数据集/表,链接表以及轻松创建新表。
答案 2 :(得分:0)
Python dict
没有.append()
方法(但是list
),您想要的是.update()
如果要使用for循环,可以尝试
# Example using for-loop
edges = {
1: [1, 11],
2: [2, 222],
3: [33],
4: [4, 4444, 44]}
categories = {
1: ['category1'],
2: ['category2', 'category3'],
3: ['category1', 'category4']}
final = {}
for key, value in categories.items():
final['category{}'.format(key)] = {}
for x in value:
edge_key = int(x.replace('category', ''))
final['category{}'.format(key)].update({edge_key: edges[edge_key]})
>>> print(final)
# Output
# {'category2': {2: [2, 222], 3: [33]}, 'category1': {1: [1, 11]}, 'category3': {1: [1, 11], 4: [4, 4444, 44]}}