<p>如果它们的长度都一样,这就很容易了。您要做的是拆分字典,然后在print语句中使用end kwarg循环打印。在</p>
<p>像下面这样的。如果你把每一项都列成一个长度相同的清单,那就更容易了。在</p>
<pre><code>def print_dict_table(result):
"""Print a dictionary of lists like a table.
Args:
result (dict): Dictionary of lists to print as a table.
"""
# Count the longest value
keys = result.keys()
count = 0
for i in result.values():
if isinstance(i, list) and len(i) > count:
count = len(i)
# print header
for i in keys:
print(i, end="\t")
print()
# print columns
for i in range(count):
for j in keys:
if isinstance(result[j], list) and len(result[j]) >= i:
print(result[j][i], end="\t")
else:
print(result[j], end="\t")
print()
# end print_dict_table
result = {'WeightedLevel': [388.850952, 716.718689, 1312.55957, 2405.087158, 4460.083984, 8543.792969, 18805.201172, 57438.140625, 1792.367554], 'Job': 'Desktop', 'LoadLevel': [0.212399, 0.393191, 0.727874, 1.347436, 2.494368, 4.617561, 8.548006, 15.824027, 1.0], 'Task': 'test', 'Failure': [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0], 'Blocks': [7255.151855, 231.589661, 9.365415, 0.55364, 0.0504, 0.006408, 0.001204, 0.000842, 2.060041]}
print_dict_table(result)
</code></pre>
<p>结果</p>
^{pr2}$
<p>词典没有排序。老实说,我会重新定义你的数据结构。如果是表数据,则使用表。在</p>