2012-09-04 61 views
5

我有一本使用Python创建的字典。漂亮的打印输出在控制台窗口中以横向树形格式输出

d = {'a': ['Adam', 'Book', 4], 'b': ['Bill', 'TV', 6, 'Jill', 'Sports', 1, 'Bill', 'Computer', 5], 'c': ['Bill', 'Sports', 3], 'd': ['Quin', 'Computer', 3, 'Adam', 'Computer', 3], 'e': ['Quin', 'TV', 2, 'Quin', 'Book', 5], 'f': ['Adam', 'Computer', 7]} 

我想以横向树形式打印出来,而不是在控制台上。我尝试过很漂亮的打印,但是当字典变长时,阅读变得困难。

例如,与本字典,它将返回:

a -> Book -> Adam -> 4 
b -> TV -> Bill -> 6 
    -> Sports -> Jill -> 1 
    -> Computer -> Bill -> 5 
c -> Sports -> Bill -> 3 
d -> Computer -> Quin -> 3 
       -> Adam -> 3 
e -> TV -> Quin -> 2 
    Book -> Quin -> 5 
f -> Computer -> Adam -> 7 

从本质上讲,漂亮的打印是通过活动,或在列表中的第二位置的项目组织,然后通过名称,然后由数。

上面的示例输出只是一个示例。我尝试使用Pretty print a tree,但无法弄清楚如何将其转换为横向格式。

回答

1
def treePrint(tree): 
    for key in tree: 
     print key, # comma prevents a newline character 
     treeElem = tree[key] # multiple lookups is expensive, even amortized O(1)! 
     for subElem in treeElem: 
      print " -> ", subElem, 
      if type(subElem) != str: # OP wants indenting after digits 
       print "\n " # newline and a space to match indenting 
     print "" # forces a newline 
+0

看起来不错不错......交涉任何方式,使其类似于漂亮的打印树一个地方有一棵树格式? – user1530318

+0

如果您正在寻找某种递归树深度解决方案,那么您可能会遇到数据不可迭代的基本情况,否则会进行递归。我不太了解pprint模块,所以我不知道你希望我能做什么。 – rsegal

+0

使用像'dict'这样的关键字的名称作为参数或变量名称被认为是不正确的形式,类型检查也是如此。 – martineau

3

下面是我该怎么做的。由于树只有两层深度 - 尽管你想要的输出格式可能似乎暗示着 - 没有必要使用递归来遍历它的内容,因为迭代运行得很好。可能这和你引用的#f代码没什么两样,因为我不知道这门语言,但它更短,更易读 - 至少对我来说。

from itertools import izip 

def print_tree(tree): 
    for key in sorted(tree.iterkeys()): 
     data = tree[key] 
     previous = data[0], data[1], data[2] 
     first = True 
     for name, activity, value in izip(*[iter(data)]*3): # groups of three 
      activity = activity if first or activity != previous[1] else ' '*len(activity) 
      print '{} ->'.format(key) if first else ' ', 
      print '{} -> {} -> {}'.format(activity, name, value) 
      previous = name, activity, value 
      first = False 

d = {'a': ['Adam', 'Book', 4], 
    'b': ['Bill', 'TV', 6, 'Jill', 'Sports', 1, 'Bill', 'Computer', 5], 
    'c': ['Bill', 'Sports', 3], 
    'd': ['Quin', 'Computer', 3, 'Adam', 'Computer', 3], 
    'e': ['Quin', 'TV', 2, 'Quin', 'Book', 5], 
    'f': ['Adam', 'Computer', 7]} 

print_tree(d) 

输出:

from itertools import izip 

def print_tree(tree): 
    for key in sorted(tree.iterkeys()): 
     data = tree[key] 
     previous = data[0], data[1], data[2] 
     first = True 
     for name, activity, value in sorted(izip(*[iter(data)]*3)): # changed 
      name = name if first or name != previous[0] else ' '*len(name) # changed 
      print '{} ->'.format(key) if first else ' ', 
      print '{} -> {} -> {}'.format(name, activity, value) # changed 
      previous = name, activity, value 
      first = False 

a -> Book -> Adam -> 4 
b -> TV -> Bill -> 6 
    Sports -> Jill -> 1 
    Computer -> Bill -> 5 
c -> Sports -> Bill -> 3 
d -> Computer -> Quin -> 3 
       -> Adam -> 3 
e -> TV -> Quin -> 2 
    Book -> Quin -> 5 
f -> Computer -> Adam -> 7 

更新

要组织的名字,而不是活动的输出你需要改变三行如下图所示

修改后的输出:

a -> Adam -> Book -> 4 
b -> Bill -> Computer -> 5 
      -> TV -> 6 
    Jill -> Sports -> 1 
c -> Bill -> Sports -> 3 
d -> Adam -> Computer -> 3 
    Quin -> Computer -> 3 
e -> Quin -> Book -> 5 
      -> TV -> 2 
f -> Adam -> Computer -> 7 
+0

第3行应该是data = tree [key],而不是d [key],对不对? – user1530318

+0

另外,如果我希望按名称而不是按活动组织的话,我该如何做,因此按列表中的第一个元素进行组织,而不是第二个元素。一个 - >亚当 - >书 - > 4例如 – user1530318

+0

是的,第3行是错误的(从我做它功能之前剩余的)。我修正了这个问题,并且更新了我的答案以显示热点,以修改它以按名称而不是按活动组织输出。 – martineau