将图边列表转换为JSON

2024-06-25 23:45:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个元组列表,其边缘来自networkx,它们保证是树型的:

[('king', 'governor'),
 ('governor', 'editor'),
 ('king', 'state'),
 ('state', 'collapse'),
 ('collapse', 'coverage'),
 ('collapse', 'author'),
 ('collapse', 'opening'),
 ('state', 'head'),
 ('state', 'lord')]

这些都是按深度优先的搜索顺序排序的,但也可以很容易地按广度优先的顺序排序,如果这样做更容易的话。在

我正在寻找一种方法来将这个边列表转换成一个JSON对象。前面的例子将变成:

^{pr2}$

叶节点应该像示例输出中那样用dict表示,还是简单地用字符串表示,这取决于您。如果有一种使用networkx.DiGraph比它的边列表更容易做到这一点,那也同样有效。在

感谢任何帮助。在


Tags: networkx列表排序顺序coverageeditor边缘author
1条回答
网友
1楼 · 发布于 2024-06-25 23:45:29
import json                                                                                                                                                                                                        

data = [('king', 'governor'),                                                                                                                                                                                      
        ('governor', 'editor'),                                                                                                                                                                                    
        ('king', 'state'),                                                                                                                                                                                         
        ('state', 'collapse'),                                                                                                                                                                                     
        ('collapse', 'coverage'),                                                                                                                                                                                  
        ('collapse', 'author'),                                                                                                                                                                                    
        ('collapse', 'opening'),                                                                                                                                                                                   
        ('state', 'head'),                                                                                                                                                                                         
        ('state', 'lord')];                                                                                                                                                                                        

root = data[0][0]                                                                                                                                                                                                  
node2chilren = {root: []}                                                                                                                                                                                          
for parent, child in data:                                                                                                                                                                                         
    childnode = {child: []}                                                                                                                                                                                        
    children = node2chilren[parent]                                                                                                                                                                                
    children.append(childnode)                                                                                                                                                                                     
    node2chilren[child] = childnode[child]                                                                                                                                                                         

jsonstr = json.dumps({root: node2chilren[root]}, indent=4)                                                                                                                                                         
print jsonstr

输出

^{pr2}$

相关问题 更多 >