2015-11-02 114 views
0

我想用Python 2.7将表中的数据转换为嵌套字典。将表格数据转换为表示树的嵌套字典

这是数据的样本 -

P C GC GGC 
-------------- 
P1 C1 GC1 GGC1 
P1 C1 GC1 GGC2 
P1 C1 GC2 GGC3 
P1 C1 GC2 GGC4 
P1 C1 GC2 GGC5 
P1 C2 GC3 GGC6 
P1 C2 GC3 GGC7 
P1 C2 GC4 GGC8 
P1 C2 GC4 GGC9 
P2 C3 GC5 GGC10 
P2 C3 GC5 GGC11 
P2 C3 GC5 GGC12 

这里的行中的列代表父母,孩子,盛大的孩子,一个伟大的GC。这意味着层次结构是4层深。 (我有一个情况,可能有5或6个级别的深度,但我不需要解决方案来动态调整级别,硬编码为4级的解决方案是好的)。

我需要将此数据转换为表示树的嵌套字典。 (稍后会进入元素的UI树视图类型)。

的预期结果是 -

[ 
    { 
    text: "P1", 
    nodes: [ 
     { 
     text: "C1", 
     nodes: [ 
      { 
      text: "GC1", 
      nodes: [ 
       { 
       text: "GGC1" 
       }, 
       { 
       text: "GGC2" 
       } 
      ] 
      }, 
      { 
      text: "GC2", 
      nodes: [ 
       { 
       text: "GGC3" 
       }, 
       { 
       text: "GGC4" 
       }, 
       { 
       text: "GGC5" 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     text: "C2", 
     nodes: [ 
      { 
      text: "GC3", 
      nodes: [ 
       { 
       text: "GGC6" 
       }, 
       { 
       text: "GGC7" 
       } 
      ] 
      }, 
      { 
      text: "GC4", 
      nodes: [ 
       { 
       text: "GGC8" 
       }, 
       { 
       text: "GGC9" 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    }, 
    { 
    text: "P2", 
    nodes: [ 
     { 
     text: "C3", 
     nodes: [ 
      { 
      text: "GC5", 
      nodes: [ 
       { 
       text: "GGC10" 
       }, 
       { 
       text: "GGC12" 
       } 
      ] 
      }, 
     ] 
     } 
    ] 
    } 
]; 

这里一排每列被转换到层次结构中的级别。父母基本上处于等级制的顶端。

解决此问题的最佳方法是什么? (使用熊猫< 0.15.1的解决方案也很好)。

PS - Python新手在这里。

+0

这不是嵌套字典! – WoodChopper

+0

@WoodChopper - 如果你有一些建议,请让我知道。如果SO允许,我可以尝试编辑该主题。 – Afp

+0

你想在那个结构中输出吗? – WoodChopper

回答

1

您可以通过DF和打印得到类似的结构重复,

我用熊猫为数据帧表,但它不是必需的。你可以使用简单的列表。

df 
Out[36]: 
     km price 1b 1c 1d 
0 240000 3650 hey yo OMG 
1 139800 3800 hey yo OMG 
2 150500 4400 hey yo OMG 
3 185530 4450 hey yo OMG 
4 176000 5250 hey yo OMG 
5 114800 5350 hey yo OMG 
6 166800 5800 hey yo OMG 
7 89000 5990 hey yo OMG 
8 144500 5999 hey yo OMG 
9 84000 6200 hey yo OMG 
10 82029 6390 hey yo OMG 



for x in df.iterrows(): 
    print '{text: "%s",nodes: [{text: "%s",nodes: [{text: "%s",nodes: [{text: "%s" },{text: "%s"}]},]}]},' % (x[1][0], x[1][1],x[1][2], x[1][3], x[1][4]) 

{text: "240000",nodes: [{text: "3650",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "139800",nodes: [{text: "3800",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "150500",nodes: [{text: "4400",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "185530",nodes: [{text: "4450",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "176000",nodes: [{text: "5250",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "114800",nodes: [{text: "5350",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "166800",nodes: [{text: "5800",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "89000",nodes: [{text: "5990",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "144500",nodes: [{text: "5999",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "84000",nodes: [{text: "6200",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
{text: "82029",nodes: [{text: "6390",nodes: [{text: "hey",nodes: [{text: "yo" },{text: "OMG"}]},]}]}, 
+0

@Afp我这回答你的问题,请不要忘记接受答案。 – zero323