簡體   English   中英

將圖邊緣列表轉換為JSON樹

[英]Convert list of graph edges to JSON tree

我有一個元組列表,其中有來自networkx邊緣,這些邊緣保證是樹狀的:

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

它們以深度優先的搜索順序進行排序,但是如果這樣會更容易以廣度優先的順序進行排序。

我正在尋找一種將邊緣列表轉換為JSON對象的方法。 前面的示例將變為:

{'king': [{'governor': [{'editor': []}]},
          {'state': [{'collapse': [{'coverage': []},
                                   {'author': []},
                                   {'opening': []}]},
                     {'head': []},
                     {'lord': []}]
           }]
}

葉節點是應像示例輸出中的dict一樣表示,還是簡單地以字符串表示,取決於您。 如果使用networkx.DiGraph比使用其邊緣列表更容易做到這一點,那么效果也很好。

任何幫助表示贊賞。

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

輸出

{
    "king": [
        {
            "governor": [
                {
                    "editor": []
                }
            ]
        }, 
        {
            "state": [
                {
                    "collapse": [
                        {
                            "coverage": []
                        }, 
                        {
                            "author": []
                        }, 
                        {
                            "opening": []
                        }
                    ]
                }, 
                {
                    "head": []
                }, 
                {
                    "lord": []
                }
            ]
        }
    ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM