亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從路徑列表轉換為 Flare json 格式?

從路徑列表轉換為 Flare json 格式?

慕少森 2021-08-05 18:18:22
我在 python 中有如下所示的數據:[['a', 'b', 'c', 50], ['a', 'b', 'd', 100], ['a', 'b', 'e', 67], ['a', 'g', 'c', 12], ['q', 'k', 'c', 11], ['q', 'b', 'p', 11]]其中列表的每個元素都是一個完整的分層路徑,最后一個元素是路徑的大小。要在 D3 中進行可視化,我需要將數據采用耀斑數據格式 - 見此處:https://github.com/d3/d3-hierarchy/blob/master/test/data/flare.json所以一小段看起來像這樣{ "name": "root", "children": [  {   "name": "a",   "children": [    {     "name": "b",     "children": [      {"name": "c", "value": 50},      {"name": "d", "value": 100},      {"name": "e", "value": 67},     ]    },    {     "name": "g",     "children": [      {"name": "c", "value": 12},     ]    },等等……從我一直在查找的內容來看,我認為該解決方案是遞歸的,并且會json在 Python 字典上使用該庫,但我似乎無法讓它發揮作用。任何幫助是極大的贊賞。
查看完整描述

3 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

這是使用遞歸的解決方案:


 def add_to_flare(n, flare):

     children = flare["children"]


     if len(n) == 2:

         children.append({"name": n[0], "value": n[1]})

     else:

         for c in children:

             if c["name"] == n[0]:

                 add_to_flare(n[1:], c)

                 return


         children.append({"name": n[0], "children": []})

         add_to_flare(n[1:], children[-1])


 flare = {"name": "root", "children": []} 


 for i in data:

     add_to_flare(i, flare)

為了很好地顯示它,我們可以使用這個json庫:


import json

print(json.dumps(flare, indent=1))


{

 "name": "root", 

 "children": [

  {

   "name": "a", 

   "children": [

    {

     "name": "b", 

     "children": [

      {

       "name": "c", 

       "value": 50

      }, 

      {

       "name": "d", 

       "value": 100

      }, 

      {

       "name": "e", 

       "value": 67

      }

     ]

    }, 

    {

     "name": "g", 

     "children": [

      {

       "name": "c", 

       "value": 12

      }

     ]

    }

   ]

  }, 

  {

   "name": "q", 

   "children": [

    {

     "name": "k", 

     "children": [

      {

       "name": "c", 

       "value": 11

      }

     ]

    }, 

    {

     "name": "b", 

     "children": [

      {

       "name": "p", 

       "value": 11

      }

     ]

    }

   ]

  }

 ]

}


查看完整回答
反對 回復 2021-08-05
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

試試這個:


master = []

for each in your_list:

    head = master

    for i in range(len(each)):

        names = [e['name'] for e in head]

        if i == len(each) - 2:

            head.append({'name': each[i], 'value': each[i+1]})

            break

        if each[i] in names:

            head = head[names.index(each[i])]['children']

        else:

            head.append({'name': each[i], 'children': []})

            head = head[-1]['children']

結果:


[{'children': [{'children': [{'name': 'c', 'value': 50},

                             {'name': 'd', 'value': 100},

                             {'name': 'e', 'value': 67}],

                'name': 'b'},

               {'children': [{'name': 'c', 'value': 12}], 'name': 'g'}],

  'name': 'a'},

 {'children': [{'children': [{'name': 'c', 'value': 11}], 'name': 'k'},

               {'children': [{'name': 'p', 'value': 11}], 'name': 'b'}],

  'name': 'q'}]

請注意name和children在本詞典中被翻轉,因為它是無序的。但最終的結構是一樣的。


把它放在根目錄下以獲得你的目標:


my_dict = {'name':'root', 'children': master}


查看完整回答
反對 回復 2021-08-05
?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

假設您的列表列表存儲在 variable 中l,您可以執行以下操作:


o = []

for s in l:

    c = o

    for i, n in enumerate(['root'] + s[:-1]):

        for d in c:

            if n == d['name']:

                break

        else:

            c.append({'name': n})

            d = c[-1]

        if i < len(s) - 1:

            if 'children' not in d:

                d['children'] = []

            c = d['children']

        else:

            d['value'] = s[-1]

這樣o[0]就變成了:


{'children': [{'children': [{'children': [{'name': 'c', 'value': 50},

                                          {'name': 'd', 'value': 100},

                                          {'name': 'e', 'value': 67}],

                             'name': 'b'},

                            {'children': [{'name': 'c', 'value': 12}],

                             'name': 'g'}],

               'name': 'a'},

              {'children': [{'children': [{'name': 'c', 'value': 11}],

                             'name': 'k'},

                            {'children': [{'name': 'p', 'value': 11}],

                             'name': 'b'}],

               'name': 'q'}],

 'name': 'root'}


查看完整回答
反對 回復 2021-08-05
  • 3 回答
  • 0 關注
  • 234 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號