我有JSON格式的数据:

data = {"outfit":{"shirt":"red,"pants":{"jeans":"blue","trousers":"khaki"}}}


我试图使用InfoVis将这些数据绘制到决策树中,因为它看起来很漂亮且具有交互性。问题在于他们的图采用以下格式的JSON数据:

data = {id:"nodeOutfit",
    name:"outfit",
    data:{},
    children:[{
        id:"nodeShirt",
        name:"shirt",
        data:{},
        children:[{
            id:"nodeRed",
            name:"red",
                   data:{},
                   children:[]
        }],
     },  {
         id:"nodePants",
         name:"pants",
         data:{},
         children:[{
             id:"nodeJeans",
             name:"jeans",
             data:{},
             children:[{
                    id:"nodeBlue",
                    name:"blue",
                    data:{},
                    children[]
             },{
             id:"nodeTrousers",
             name:"trousers",
             data:{},
             children:[{
                    id:"nodeKhaki",
                    name:"khaki",
                    data:{},
                    children:[]
          }
    }


请注意,在每个键和值上都添加了“ id”,“ data”和“ children”,并将每个键和值都称为“名称”。我觉得我必须编写一个递归函数来添加这些额外的值。是否有捷径可寻?

这是我想要做的,但是我不确定这是否正确。遍历所有键和值,并用相应的键和值替换它们:

for name, list in data.iteritems():
    for dict in list:
        for key, value in dict.items():
            #Need something here which changes the value for each key and values
            #Not sure about the syntax to change "outfit" to name:"outfit" as well as
            #adding id:"nodeOutfit", data:{}, and 'children' before the value


让我知道我是否要离开。

这是他们的示例http://philogb.github.com/jit/static/v20/Jit/Examples/Spacetree/example1.html

这是数据http://philogb.github.com/jit/static/v20/Jit/Examples/Spacetree/example1.code.html

最佳答案

一个简单的递归解决方案:

data = {"outfit":{"shirt":"red","pants":{"jeans":"blue","trousers":"khaki"}}}
import json
from collections import OrderedDict

def node(name, children):
    n = OrderedDict()
    n['id'] = 'node' + name.capitalize()
    n['name'] = name
    n['data'] = {}
    n['children'] = children
    return n

def convert(d):
    if type(d) == dict:
        return [node(k, convert(v)) for k, v in d.items()]
    else:
        return [node(d, [])]

print(json.dumps(convert(data), indent=True))


请注意,convert返回一个list,而不是dict,因为data还可以具有一个以上的键,而不仅仅是'outfit'

输出:

[
 {
  “ id”:“ nodeOutfit”,
  “ name”:“衣服”,
  “数据”:{},
  “儿童”:[
   {
    “ id”:“ nodeShirt”,
    “ name”:“衬衫”,
    “数据”:{},
    “儿童”:[
     {
      “ id”:“ nodeRed”,
      “ name”:“红色”,
      “数据”:{},
      “儿童”:[]
     }
    ]
   },
   {
    “ id”:“ nodePants”,
    “ name”:“裤子”,
    “数据”:{},
    “儿童”:[
     {
      “ id”:“ nodeJeans”,
      “ name”:“牛仔裤”,
      “数据”:{},
      “儿童”:[
       {
        “ id”:“ nodeBlue”,
        “ name”:“ blue”,
        “数据”:{},
        “儿童”:[]
       }
      ]
     },
     {
      “ id”:“ nodeTrousers”,
      “ name”:“裤子”,
      “数据”:{},
      “儿童”:[
       {
        “ id”:“ nodeKhaki”,
        “ name”:“卡其色”,
        “数据”:{},
        “儿童”:[]
       }
      ]
     }
    ]
   }
  ]
 }
]

关于python - 在Python中处理数据结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13727629/

10-11 14:59
查看更多