在python中以树结构显示词典?
我有一本字典,我想以特定的格式显示它 这是我的字典:在python中以树结构显示词典?,python,dictionary,Python,Dictionary,我有一本字典,我想以特定的格式显示它 这是我的字典: tree = { 'wesley': { '1': { 'romulan': { '1': '0', '0': '1' } }, '0': { 'romulan': { '1': '0', '0': { 'poetry': { '1': { 'h
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'
}
}
}
}
}
}
}
}
我想将其显示为:
wesley=1:
|罗慕兰=1:0
|罗慕兰=0:1
韦斯利=0:
|罗慕兰=1:0
|罗慕兰=0:
||诗歌=1:
|| |荣誉=1:0
|| |荣誉=0:1
||诗歌=0:
|| |荣誉=1:1
|| |荣誉=0:0
我对字典和Python非常陌生,我不知道如何显示它们。这将使它在没有递归的情况下非常接近,尽管它可能很脆弱
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
| |
|
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
您可以通过调用
.replace()
来获得正确的结果。这将准确地输出您想要的结果
# 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)
输出:
(.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
| |
|
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的新手,我不会从这样的事情开始
pprint.pprint
通常足以进行基本检查,在深入研究更复杂的主题之前,您需要先学习基础知识。这是一个非常特殊的问题,您编写的代码在其他方面基本上是无用的,而且它涉及到一些您可能不熟悉的概念。我很担心这一点,谢谢。也许您应该解释一下为什么需要这样做?你有一些奇怪的数据结构和基于字符串的数字索引…你不能只使用一个列表吗<代码>{wesley:[{…},{…},{…}]}。非常好而且使用简单