2016-09-23 5 views
0

私は辞書があり、それを特定の形式で表示したいのですが。辞書をツリーとして表示しますか?

これは私の辞書です:

tree = { 
    'wesley': { 
    '1': { 
     'romulan': { 
     '1': '0', 
     '0': '1' 
     } 
    }, 
    '0': { 
     'romulan': { 
     '1': '0', 
     '0': { 
      'poetry': { 
      '1': { 
       'honor': { 
       '1': '0', 
       '0': '1' 
       } 
      }, 
      '0': { 
       'honor': { 
       '1': '1', 
       '0': '0' 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

私はそれを表示したいと:私は非常に辞書に新しいとPythonと私はそれらを表示する方法の知識がない

wesley = 1 : 
| romulan = 1 : 0 
| romulan = 0 : 1 
wesley = 0 : 
| romulan = 1 : 0 
| romulan = 0 : 
| | poetry = 1 : 
| | | honor = 1 : 0 
| | | honor = 0 : 1 
| | poetry = 0: 
| | | honor = 1 : 1 
| | | honor = 0 : 0 

+3

私は確認することはできませんが、私はあなたが何をしたいのマッチング標準出力形式を見たことがありません。これを行うには、独自のコードをロールバックする必要があります。それを正しく行うにはおそらく再帰関数が必要になるでしょう。あなたがPythonの初心者であれば、私はこのようなものから始めません。 'pprint.pprint'は通常、基本的な検査には十分ですが、より複雑なものに入る前に基礎を学ぶ必要があります。これは信じられないほど特殊な問題です。あなたが書いたコードは他の目的のためにはほとんど役に立ちません。あなたにはおそらくよく慣れていないいくつかの概念が関係しています。 – ShadowRanger

+0

私はこれを恐れていましたが、とにかく感謝しています。 –

+0

なぜこのように必要なのか説明してください。文字列ベースの数値インデックスで構造化した奇妙なデータがあります...それらのリストを使うことはできませんか? '{ウェズリー:[{...}、{...}、{...}}}'。 – Soviut

答えて

2

これは、潜在的に脆い

import json 

tree = {'wesley': {'1': {'romulan': {'1': '0', '0': '1'}}, '0': {'romulan': {'1': '0', '0': {'poetry': {'1': {'honor': {'1': '0', '0': '1'}}, '0': {'honor': {'1': '1', '0': '0'}}}}}}}} 


tree_str = json.dumps(tree, indent=4) 
tree_str = tree_str.replace("\n ", "\n") 
tree_str = tree_str.replace('"', "") 
tree_str = tree_str.replace(',', "") 
tree_str = tree_str.replace("{", "") 
tree_str = tree_str.replace("}", "") 
tree_str = tree_str.replace(" ", " | ") 
tree_str = tree_str.replace(" ", " ") 

print(tree_str) 

出力だが、再帰のない、それは非常に近い取得します:

(.venv35) ➜ stackoverflow python weird_formated_print.py 

wesley: 
| 0: 
| | romulan: 
| | | 0: 
| | | | poetry: 
| | | | | 0: 
| | | | | | honor: 
| | | | | | | 0: 0 
| | | | | | | 1: 1 
| | | | | | 
| | | | | 
| | | | | 1: 
| | | | | | honor: 
| | | | | | | 0: 1 
| | | | | | | 1: 0 
| | | | | | 
| | | | | 
| | | | 
| | | 
| | | 1: 0 
| | 
| 
| 1: 
| | romulan: 
| | | 0: 1 
| | | 1: 0 
| | 
| 

あなたはそれだけで権利を取得するために.replace()呼び出しで遊ぶことができます。

+0

ありがとうございます! –

2

これは、あなたが望むものを正確に出力します。

# If you are not using Python 3 
from __future__ import print_function 

tree = {'wesley': {'1': {'romulan': {'1': '0', '0': '1'}}, '0': {'romulan': {'1': '0', '0': {'poetry': {'1': {'honor': {'1': '0', '0': '1'}}, '0': {'honor': {'1': '1', '0': '0'}}}}}}}} 

def is_number(s): 
    try: 
     float(s) 
     return True 
    except ValueError: 
     return False 

def go(dic, last_key, current_level): 
    for key, value in dic.items(): 
     if is_number(key): 
      for i in range(current_level - 1): 
       print("| ", end="") 

      print(last_key, "= ", end="") 
      print(key, ": ", end="") 
     else: 
      if current_level > 0: 
       print("") 

      current_level = current_level + 1 

     if isinstance(value, dict): 
      go(value, key, current_level) 
     else: 
      print(value) 

go(tree, None, 0) 

出力:

wesley = 1 : 
| romulan = 1 : 0 
| romulan = 0 : 1 
wesley = 0 : 
| romulan = 1 : 0 
| romulan = 0 : 
| | poetry = 1 : 
| | | honor = 1 : 0 
| | | honor = 0 : 1 
| | poetry = 0 : 
| | | honor = 1 : 1 
| | | honor = 0 : 0 
関連する問題