Python 漂亮的列表字典,缩写长列表
Python pretty print dictionary of lists, abbreviate long lists
我有一本列表字典,列表很长。如何以仅显示列表中的几个元素的方式打印它?显然,我可以为此编写一个自定义函数,但是否有任何内置方法或库可以实现这一点?例如,当打印大型数据帧时,pandas
可以用很短的方式很好地打印它。
这个例子更好地说明了我的意思:
obj = {'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
'WL3TWSDP8E',
'LD8QY7DMJ3',
'J36U3Z9KOQ',
'KU2FUGYB2U',
'JF3RQ315BY'],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
'KWSJA5WDKQ',
'WX9SVRFO0G',
'6UN63WU64G',
'3Z89U7XM60',
'167CYON6YN']}
期望的输出:像这样:
{'key_1':
['EG8XYD9FVN', 'S2WARDCVAO', '...'],
'key_2':
['162LO154PM', '3ROAV881V2', '...']
}
我写的这个递归函数可以满足你的要求。你也可以选择你想要的缩进
def pretty(d, indent=0):
for key in sorted(d.keys()):
print '\t' * indent + str(key)
if isinstance(d[key], dict):
pretty(d[key], indent+1)
else:
print '\t' * (indent+1) + str(d[key])
你的字典的输出是:
key_1
['EG8XYD9FVN', 'S2WARDCVAO', 'J00YCU55DP', 'R07BUIF2F7', 'VGPS1JD0UM', 'WL3TWSDP8E', 'LD8QY7DMJ3', 'J36U3Z9KOQ', 'KU2FUGYB2U', 'JF3RQ315BY']
key_2
['162LO154PM', '3ROAV881V2', 'I4T79LP18J', 'WBD36EM6QL', 'DEIODVQU46', 'KWSJA5WDKQ', 'WX9SVRFO0G', '6UN63WU64G', '3Z89U7XM60', '167CYON6YN']
您可以使用 pprint
模块:
pprint.pprint(obj)
会输出:
{'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
'WL3TWSDP8E',
'LD8QY7DMJ3',
'J36U3Z9KOQ',
'KU2FUGYB2U',
'JF3RQ315BY'],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
'KWSJA5WDKQ',
'WX9SVRFO0G',
'6UN63WU64G',
'3Z89U7XM60',
'167CYON6YN']}
而且,
pprint.pprint(obj,depth=1)
会输出:
{'key_1': [...], 'key_2': [...]}
并且,
pprint.pprint(obj,compact=True)
会输出:
{'key_1': ['EG8XYD9FVN', 'S2WARDCVAO', 'J00YCU55DP', 'R07BUIF2F7',
'VGPS1JD0UM', 'WL3TWSDP8E', 'LD8QY7DMJ3', 'J36U3Z9KOQ',
'KU2FUGYB2U', 'JF3RQ315BY'],
'key_2': ['162LO154PM', '3ROAV881V2', 'I4T79LP18J', 'WBD36EM6QL',
'DEIODVQU46', 'KWSJA5WDKQ', 'WX9SVRFO0G', '6UN63WU64G',
'3Z89U7XM60', '167CYON6YN']}
使用reprlib。格式不是那么漂亮,但实际上是缩写。
> import repr
> repr.repr(map(lambda _: range(100000), range(10)))
'[[0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], ...]'
> repr.repr(dict(map(lambda i: (i, range(100000)), range(10))))
'{0: [0, 1, 2, 3, 4, 5, ...], 1: [0, 1, 2, 3, 4, 5, ...], 2: [0, 1, 2, 3, 4, 5, ...], 3: [0, 1, 2, 3, 4, 5, ...], ...}'
If it were not for the pretty printing, the reprlib
module would be the way: Safe, elegant and customizable handling of deeply nested and recursive / self-referencing data structures is what it has what.
然而,事实证明将 reprlib
和 pprint
模块结合起来并不是微不足道的,至少我无法想出一个干净的在不破坏(某些)漂亮印刷方面的方式。
因此,这里有一个解决方案,只需 classes PrettyPrinter
根据需要裁剪/缩写列表:
from pprint import PrettyPrinter
obj = {
'key_1': [
'EG8XYD9FVN', 'S2WARDCVAO', 'J00YCU55DP', 'R07BUIF2F7', 'VGPS1JD0UM',
'WL3TWSDP8E', 'LD8QY7DMJ3', 'J36U3Z9KOQ', 'KU2FUGYB2U', 'JF3RQ315BY',
],
'key_2': [
'162LO154PM', '3ROAV881V2', 'I4T79LP18J', 'WBD36EM6QL', 'DEIODVQU46',
'KWSJA5WDKQ', 'WX9SVRFO0G', '6UN63WU64G', '3Z89U7XM60', '167CYON6YN',
],
# Test case to make sure we didn't break handling of recursive structures
'key_3': [
'162LO154PM', '3ROAV881V2', [1, 2, ['a', 'b', 'c'], 3, 4, 5, 6, 7],
'KWSJA5WDKQ', 'WX9SVRFO0G', '6UN63WU64G', '3Z89U7XM60', '167CYON6YN',
]
}
class CroppingPrettyPrinter(PrettyPrinter):
def __init__(self, *args, **kwargs):
self.maxlist = kwargs.pop('maxlist', 6)
return PrettyPrinter.__init__(self, *args, **kwargs)
def _format(self, obj, stream, indent, allowance, context, level):
if isinstance(obj, list):
# If object is a list, crop a copy of it according to self.maxlist
# and append an ellipsis
if len(obj) > self.maxlist:
cropped_obj = obj[:self.maxlist] + ['...']
return PrettyPrinter._format(
self, cropped_obj, stream, indent,
allowance, context, level)
# Let the original implementation handle anything else
# Note: No use of super() because PrettyPrinter is an old-style class
return PrettyPrinter._format(
self, obj, stream, indent, allowance, context, level)
p = CroppingPrettyPrinter(maxlist=3)
p.pprint(obj)
输出 maxlist=3
:
{'key_1': ['EG8XYD9FVN', 'S2WARDCVAO', 'J00YCU55DP', '...'],
'key_2': ['162LO154PM',
'3ROAV881V2',
[1, 2, ['a', 'b', 'c'], '...'],
'...']}
使用 maxlist=5
的输出(触发在不同行上拆分列表):
{'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
'...'],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
'...'],
'key_3': ['162LO154PM',
'3ROAV881V2',
[1, 2, ['a', 'b', 'c'], 3, 4, '...'],
'KWSJA5WDKQ',
'WX9SVRFO0G',
'...']}
备注:
- 这将创建份 个列表。根据数据结构的大小,这在内存使用方面可能非常昂贵。
- 这只处理 lists 的特殊情况。必须为字典、元组、集合、frozensets 等实现等效行为,才能使 class 具有通用性。
你可以使用 IPython.lib.pretty.
from IPython.lib.pretty import pprint
> pprint(obj, max_seq_length=5)
{'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
...],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
...]}
> pprint(dict(map(lambda i: (i, range(i + 5)), range(100))), max_seq_length=10)
{0: [0, 1, 2, 3, 4],
1: [0, 1, 2, 3, 4, 5],
2: [0, 1, 2, 3, 4, 5, 6],
3: [0, 1, 2, 3, 4, 5, 6, 7],
4: [0, 1, 2, 3, 4, 5, 6, 7, 8],
5: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
6: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...],
7: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...],
8: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...],
9: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...],
...}
对于旧版本的 IPython,您可以利用 RepresentationPrinter:
from IPython.lib.pretty import RepresentationPrinter
import sys
def compact_pprint(obj, max_seq_length=10):
printer = RepresentationPrinter(sys.stdout)
printer.max_seq_length = max_seq_length
printer.pretty(obj)
printer.flush()
我有一本列表字典,列表很长。如何以仅显示列表中的几个元素的方式打印它?显然,我可以为此编写一个自定义函数,但是否有任何内置方法或库可以实现这一点?例如,当打印大型数据帧时,pandas
可以用很短的方式很好地打印它。
这个例子更好地说明了我的意思:
obj = {'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
'WL3TWSDP8E',
'LD8QY7DMJ3',
'J36U3Z9KOQ',
'KU2FUGYB2U',
'JF3RQ315BY'],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
'KWSJA5WDKQ',
'WX9SVRFO0G',
'6UN63WU64G',
'3Z89U7XM60',
'167CYON6YN']}
期望的输出:像这样:
{'key_1':
['EG8XYD9FVN', 'S2WARDCVAO', '...'],
'key_2':
['162LO154PM', '3ROAV881V2', '...']
}
我写的这个递归函数可以满足你的要求。你也可以选择你想要的缩进
def pretty(d, indent=0):
for key in sorted(d.keys()):
print '\t' * indent + str(key)
if isinstance(d[key], dict):
pretty(d[key], indent+1)
else:
print '\t' * (indent+1) + str(d[key])
你的字典的输出是:
key_1
['EG8XYD9FVN', 'S2WARDCVAO', 'J00YCU55DP', 'R07BUIF2F7', 'VGPS1JD0UM', 'WL3TWSDP8E', 'LD8QY7DMJ3', 'J36U3Z9KOQ', 'KU2FUGYB2U', 'JF3RQ315BY']
key_2
['162LO154PM', '3ROAV881V2', 'I4T79LP18J', 'WBD36EM6QL', 'DEIODVQU46', 'KWSJA5WDKQ', 'WX9SVRFO0G', '6UN63WU64G', '3Z89U7XM60', '167CYON6YN']
您可以使用 pprint
模块:
pprint.pprint(obj)
会输出:
{'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
'WL3TWSDP8E',
'LD8QY7DMJ3',
'J36U3Z9KOQ',
'KU2FUGYB2U',
'JF3RQ315BY'],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
'KWSJA5WDKQ',
'WX9SVRFO0G',
'6UN63WU64G',
'3Z89U7XM60',
'167CYON6YN']}
而且,
pprint.pprint(obj,depth=1)
会输出:
{'key_1': [...], 'key_2': [...]}
并且,
pprint.pprint(obj,compact=True)
会输出:
{'key_1': ['EG8XYD9FVN', 'S2WARDCVAO', 'J00YCU55DP', 'R07BUIF2F7',
'VGPS1JD0UM', 'WL3TWSDP8E', 'LD8QY7DMJ3', 'J36U3Z9KOQ',
'KU2FUGYB2U', 'JF3RQ315BY'],
'key_2': ['162LO154PM', '3ROAV881V2', 'I4T79LP18J', 'WBD36EM6QL',
'DEIODVQU46', 'KWSJA5WDKQ', 'WX9SVRFO0G', '6UN63WU64G',
'3Z89U7XM60', '167CYON6YN']}
使用reprlib。格式不是那么漂亮,但实际上是缩写。
> import repr
> repr.repr(map(lambda _: range(100000), range(10)))
'[[0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], [0, 1, 2, 3, 4, 5, ...], ...]'
> repr.repr(dict(map(lambda i: (i, range(100000)), range(10))))
'{0: [0, 1, 2, 3, 4, 5, ...], 1: [0, 1, 2, 3, 4, 5, ...], 2: [0, 1, 2, 3, 4, 5, ...], 3: [0, 1, 2, 3, 4, 5, ...], ...}'
If it were not for the pretty printing, the reprlib
module would be the way: Safe, elegant and customizable handling of deeply nested and recursive / self-referencing data structures is what it has what.
然而,事实证明将 reprlib
和 pprint
模块结合起来并不是微不足道的,至少我无法想出一个干净的在不破坏(某些)漂亮印刷方面的方式。
因此,这里有一个解决方案,只需 classes PrettyPrinter
根据需要裁剪/缩写列表:
from pprint import PrettyPrinter
obj = {
'key_1': [
'EG8XYD9FVN', 'S2WARDCVAO', 'J00YCU55DP', 'R07BUIF2F7', 'VGPS1JD0UM',
'WL3TWSDP8E', 'LD8QY7DMJ3', 'J36U3Z9KOQ', 'KU2FUGYB2U', 'JF3RQ315BY',
],
'key_2': [
'162LO154PM', '3ROAV881V2', 'I4T79LP18J', 'WBD36EM6QL', 'DEIODVQU46',
'KWSJA5WDKQ', 'WX9SVRFO0G', '6UN63WU64G', '3Z89U7XM60', '167CYON6YN',
],
# Test case to make sure we didn't break handling of recursive structures
'key_3': [
'162LO154PM', '3ROAV881V2', [1, 2, ['a', 'b', 'c'], 3, 4, 5, 6, 7],
'KWSJA5WDKQ', 'WX9SVRFO0G', '6UN63WU64G', '3Z89U7XM60', '167CYON6YN',
]
}
class CroppingPrettyPrinter(PrettyPrinter):
def __init__(self, *args, **kwargs):
self.maxlist = kwargs.pop('maxlist', 6)
return PrettyPrinter.__init__(self, *args, **kwargs)
def _format(self, obj, stream, indent, allowance, context, level):
if isinstance(obj, list):
# If object is a list, crop a copy of it according to self.maxlist
# and append an ellipsis
if len(obj) > self.maxlist:
cropped_obj = obj[:self.maxlist] + ['...']
return PrettyPrinter._format(
self, cropped_obj, stream, indent,
allowance, context, level)
# Let the original implementation handle anything else
# Note: No use of super() because PrettyPrinter is an old-style class
return PrettyPrinter._format(
self, obj, stream, indent, allowance, context, level)
p = CroppingPrettyPrinter(maxlist=3)
p.pprint(obj)
输出 maxlist=3
:
{'key_1': ['EG8XYD9FVN', 'S2WARDCVAO', 'J00YCU55DP', '...'],
'key_2': ['162LO154PM',
'3ROAV881V2',
[1, 2, ['a', 'b', 'c'], '...'],
'...']}
使用 maxlist=5
的输出(触发在不同行上拆分列表):
{'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
'...'],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
'...'],
'key_3': ['162LO154PM',
'3ROAV881V2',
[1, 2, ['a', 'b', 'c'], 3, 4, '...'],
'KWSJA5WDKQ',
'WX9SVRFO0G',
'...']}
备注:
- 这将创建份 个列表。根据数据结构的大小,这在内存使用方面可能非常昂贵。
- 这只处理 lists 的特殊情况。必须为字典、元组、集合、frozensets 等实现等效行为,才能使 class 具有通用性。
你可以使用 IPython.lib.pretty.
from IPython.lib.pretty import pprint
> pprint(obj, max_seq_length=5)
{'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
...],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
...]}
> pprint(dict(map(lambda i: (i, range(i + 5)), range(100))), max_seq_length=10)
{0: [0, 1, 2, 3, 4],
1: [0, 1, 2, 3, 4, 5],
2: [0, 1, 2, 3, 4, 5, 6],
3: [0, 1, 2, 3, 4, 5, 6, 7],
4: [0, 1, 2, 3, 4, 5, 6, 7, 8],
5: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
6: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...],
7: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...],
8: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...],
9: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...],
...}
对于旧版本的 IPython,您可以利用 RepresentationPrinter:
from IPython.lib.pretty import RepresentationPrinter
import sys
def compact_pprint(obj, max_seq_length=10):
printer = RepresentationPrinter(sys.stdout)
printer.max_seq_length = max_seq_length
printer.pretty(obj)
printer.flush()