2016-09-21 9 views
-2

すべての可能なキーと値のペアを含むリストに、Python辞書を変換したいと思います。辞書が似ている場合たとえば、私は再帰関数を書くのに苦労していますPython辞書を一意のキーと値のペアに変換する

[ 
    { "x": "a1", "b": "c1", "d": "e1" }, 
    { "x": "a1", "b": "c1", "d": "e2" }, 
    { "x": "a1", "b": "c2", "d": "e3" }, 
    { "x": "a1", "b": "c2", "d": "e4" }, 
    { "x": "a2", "b": "c3", "d": "e1" }, 
    { "x": "a2", "b": "c3", "d": "e5" }, 
    { "x": "a2", "b": "c4", "d": "e6" } 
] 

{ 
    "x": { 
      "a1": { "b": { 
          "c1": { "d": { "e1": {}, "e2": {} } }, 
          "c2": { "d": { "e3": {}, "e4": {} } } 
         } 
       }, 
      "a2": { "b": { 
          "c3": { "d": { "e1": {}, "e5": {} } }, 
          "c4": { "d": { "e6": {} } } 
         } 
       } 
     } 
} 

私のようなリストを取得したいと思います。 私はこれを書かれているが、それは

def get_list(groups, partial_row): 
    row = [] 
    for k, v in groups.items(): 
     if isinstance(v, dict): 
      for k2, v2 in v.items(): 
       partial_row.update({k: k2}) 
       if isinstance(v2, dict): 
        row.extend(get_list(v2, partial_row)) 
       else: 
        row.append(partial_row) 

    return row 
+1

これまで自分で何を書いていますか? –

+1

詳細を追加するために質問を編集してください。コメントに記入しないでください。 –

答えて

0
a = { 
"x": { 
    "a1": { "b": { 
        "c1": { "d": { "e1": {}, "e2": {} } }, 
        "c2": { "d": { "e3": {}, "e4": {} } } 
       } 
     }, 
    "a2": { "b": { 
        "c3": { "d": { "e1": {}, "e5": {} } }, 
        "c4": { "d": { "e6": {} } } 
       } 
     } 
} 
} 


def print_dict(d, depth, *arg): 
    if type(d) == dict and len(d): 
     for key in d: 
      if not len(arg): 
       new_arg = key 
      else: 
       new_arg = arg[0] + (': ' if depth % 2 else ', ') + key 
      print_dict(d[key], depth+1, new_arg) 
    else: 
     print(arg[0]) 

print_dict(a, depth=0) 

結果に動作していない:

x: a1, b: c1, d: e1 
x: a1, b: c1, d: e2 
x: a1, b: c2, d: e4 
x: a1, b: c2, d: e3 
x: a2, b: c4, d: e6 
x: a2, b: c3, d: e1 
x: a2, b: c3, d: e5 
1

代替ソリューション:

from pprint import pprint 
dic = { 
    "x": { 
      "a1": { "b": { 
          "c1": { "d": { "e1": {}, "e2": {} } }, 
          "c2": { "d": { "e3": {}, "e4": {} } } 
         } 
       }, 
      "a2": { "b": { 
          "c3": { "d": { "e1": {}, "e5": {} } }, 
          "c4": { "d": { "e6": {} } } 
         } 
       } 
     } 
} 


def rec(dic, path=[], all_results=[]): 
    if not dic: 
     # No items in the dictionary left, add the path 
     # up to this point to all_results 

     # This is based on the assumption that there is an even 
     # number of items in the path, otherwise you output format 
     # makes no sense 
     even_items = path[::2] 
     odd_items = path[1::2] 
     result = dict(zip(even_items, odd_items)) 
     all_results.append(result) 
     return all_results 

    for key in dic: 
     # Make a copy of the current path 
     path_cp = list(path) 
     path_cp.append(key) 
     all_results = rec(dic[key], path_cp, all_results) 

    return all_results 


results = rec(dic) 

pprint(results) 

出力:

[{'b': 'c2', 'd': 'e4', 'x': 'a1'}, 
{'b': 'c2', 'd': 'e3', 'x': 'a1'}, 
{'b': 'c1', 'd': 'e1', 'x': 'a1'}, 
{'b': 'c1', 'd': 'e2', 'x': 'a1'}, 
{'b': 'c3', 'd': 'e5', 'x': 'a2'}, 
{'b': 'c3', 'd': 'e1', 'x': 'a2'}, 
{'b': 'c4', 'd': 'e6', 'x': 'a2'}] 
+0

アイデアありがとう。出来た。ここに含まれている情報に基づいてコードを更新することができました。 – MikeG

0

あなたがあなたの元の溶液中に、次のセクション内の1つの条件チェックが欠落しています

if isinstance(v2, dict): 
    row.extend(get_list(v2, partial_row)) 
else: 
    row.append(partial_row) 

むしろそれが原因行方不明and v2

if isinstance(v2, dict) and v2: 
    row.extend(get_list(v2, partial_row)) 
else: 
    row.append(partial_row) 

にする必要があり、elseブロックが実行されていないし、あなたを取得します決して結果として常に空のリストが得られます。

+0

私はそれを前に試しました。 「部分的な経路」が詰まっていたために機能しませんでした。それが問題でした。 @Mauriceの解決策がそれを世話しました。 – MikeG

関連する問題