为了账号安全,请及时绑定邮箱和手机立即绑定

从路径列表转换为 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 关注
  • 200 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号