【问题标题】:Print a dictionary of lists vertically垂直打印列表字典
【发布时间】:2014-06-29 17:38:14
【问题描述】:

我想垂直打印以下列表字典:

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]}

所以它应该是这样的:

Job           Task          LoadLevel         Blocks          Failure         WeightedLevel
Desktop       test          4546543           4384284         0,46544564      0,1354385
                            474454            978456          2               9655
                            9655              55654           966             665 

等等……

我尝试了一些我在网上找到的代码,但它们仍然水平打印结果:

for k, d in result.items():
    print(k + ":", d)         

print("\t".join(str(x) for x in result))

【问题讨论】:

  • edit提出问题以显示您拥有的代码,并解释它究竟如何不适合您。

标签: python list python-3.x dictionary


【解决方案1】:

漂亮的打印表格需要大量代码(table-recipepretty-table)。临时编写这种代码并不好玩;您不妨使用设计良好的模块。

如果你有pandas,你可以将dict直接转储到DataFrame中,然后像这样打印:

In [4]: import pandas as pd
In [5]: 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]}

In [6]: pd.DataFrame(result)
Out[6]: 
        Blocks  Failure      Job  LoadLevel  Task  WeightedLevel
0  7255.151855        2  Desktop   0.212399  test     388.850952
1   231.589661        2  Desktop   0.393191  test     716.718689
2     9.365415        2  Desktop   0.727874  test    1312.559570
3     0.553640        2  Desktop   1.347436  test    2405.087158
4     0.050400        2  Desktop   2.494368  test    4460.083984
5     0.006408        2  Desktop   4.617561  test    8543.792969
6     0.001204        2  Desktop   8.548006  test   18805.201172
7     0.000842        2  Desktop  15.824027  test   57438.140625
8     2.060041        2  Desktop   1.000000  test    1792.367554

[9 rows x 6 columns]

这是一种无需使用第三方模块即可以表格格式打印 dict 的方法:

import itertools as IT

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]}

matrix = zip(*[value if isinstance(value, list) else IT.repeat(value) for key,value in result.items()])
print(''.join(['{:15}'.format(key) for key in result.keys()]))
for row in matrix:
    print(''.join(['{:15}'.format(str(item)) for item in row]))

产量

Task           Blocks         LoadLevel      Failure        Job            WeightedLevel  
test           7255.151855    0.212399       2.0            Desktop        388.850952     
test           231.589661     0.393191       2.0            Desktop        716.718689     
test           9.365415       0.727874       2.0            Desktop        1312.55957     
test           0.55364        1.347436       2.0            Desktop        2405.087158    
test           0.0504         2.494368       2.0            Desktop        4460.083984    
test           0.006408       4.617561       2.0            Desktop        8543.792969    
test           0.001204       8.548006       2.0            Desktop        18805.201172   
test           0.000842       15.824027      2.0            Desktop        57438.140625   
test           2.060041       1.0            2.0            Desktop        1792.367554    

【讨论】:

  • import pandas as pd ImportError: No module named 'pandas'
  • @ViharChervenkov: pip install pandas
  • 谢谢!我会下载的,但是如果没有其他想法我会接受你的回答而无需下载...
  • 我的错误。在 Python3 中,它应该只是 items,而不是 iteritems
  • @Rekovni: import itertools as ITIT 定义为内置的itertools 模块。
【解决方案2】:

如果它们的长度都相同,这将很容易。您要做的是拆分字典,然后在 print 语句中使用 end kwarg 循环打印。

如下所示。如果你把每个项目都做成一个长度相同的列表,那就更容易了。

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)

结果

Failure Job LoadLevel   WeightedLevel   Task    Blocks  
2.0 Desktop 0.212399    388.850952  test    7255.151855 
2.0 Desktop 0.393191    716.718689  test    231.589661  
2.0 Desktop 0.727874    1312.55957  test    9.365415    
2.0 Desktop 1.347436    2405.087158 test    0.55364 
2.0 Desktop 2.494368    4460.083984 test    0.0504  
2.0 Desktop 4.617561    8543.792969 test    0.006408    
2.0 Desktop 8.548006    18805.201172    test    0.001204    
2.0 Desktop 15.824027   57438.140625    test    0.000842    
2.0 Desktop 1.0 1792.367554 test    2.060041    

字典没有排序。老实说,我会重新定义您的数据结构。如果是表格数据,则使用表格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2016-02-24
    • 2018-05-04
    • 1970-01-01
    • 2015-10-03
    • 2022-01-07
    相关资源
    最近更新 更多