首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python的树结构中显示字典?

在python的树结构中显示字典?
EN

Stack Overflow用户
提问于 2016-09-23 02:27:51
回答 2查看 4.2K关注 0票数 2

我有一本字典,我想用一种特定的格式显示它。

这是我的字典:

代码语言:javascript
复制
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'
              }
            }
          }
        }
      }
    }
  }
}

我想把它展示为:

代码语言:javascript
复制
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

我对字典和Python非常陌生,我对如何显示它们一无所知。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-23 03:05:33

这将使它在没有递归的情况下非常接近,尽管它可能很脆弱。

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
    (.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()调用中四处游玩,以得到正确的结果。

票数 5
EN

Stack Overflow用户

发布于 2016-09-23 04:00:22

这将完全输出您想要的结果。

代码语言:javascript
复制
# 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)

输出:

代码语言:javascript
复制
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
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39651638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档