2015-11-02 78 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

相關問題