【发布时间】:2014-01-29 01:17:27
【问题描述】:
我正在使用pprint 很好地打印dict,它工作正常。现在我切换到使用模块collections 中的OrderedDict。不幸的是,pprint 路由似乎并没有认识到这些对象或多或少也是dicts,并且回退到将其打印为长行。
>>> d = { i:'*'*i for i in range(8) }
>>> pprint.pprint(d)
{0: '',
1: '*',
2: '**',
3: '***',
4: '****',
5: '*****',
6: '******',
7: '*******'}
>>> pprint.pprint(collections.OrderedDict(d))
OrderedDict([(0, ''), (1, '*'), (2, '**'), (3, '***'), (4, '****'), (5, '*****'), (6, '******'), (7, '*******')])
还有什么方法可以更好地表示OrderedDicts?也许即使它们嵌套在内部普通的dict 或list?
【问题讨论】:
标签: python ordereddictionary pprint