【问题标题】:How to present a nested dictionary as a table如何将嵌套字典呈现为表格
【发布时间】:2019-11-22 15:47:03
【问题描述】:

我有以下字典:

我想将这些字典打印出来,以便它们出现在一个易于阅读的表格中,像这样不使用 pandas

到目前为止我所拥有的:

我可以打印表格标题,但在映射其他列时遇到问题。

这是要复制的文本字典:

{'ffa': {'cope1': 0.6525,
         'cope2': 0.4146,
         'cope3': 0.5896,
         'cope4': 0.1521,
         'cope5': 0.5317},
 'lingual': {'cope1': -0.08865060000000001,
             'cope2': -0.150985,
             'cope3': -0.162005,
             'cope4': -0.130845,
             'cope5': -0.126411},
 'ppa': {'cope1': 0.74836,
         'cope2': 0.9444,
         'cope3': 0.300482,
         'cope4': 1.12435,
         'cope5': 0.8332200000000001}}

【问题讨论】:

  • “平均值”列是否表示所有“cope1”的平均值?您能否也将字典粘贴为文本而不是图像,以便其他人可以轻松复制它?
  • 我已经添加了字典。在将它们添加到字典之前,我已经计算了平均值,因此表格应该只显示 FFA 的值 - Cope1 等等
  • 因此对于字典中的每个列表都会在平均列中打印 Cope1 - 5 的结果,只需按原样使用它们,每个列表的名称应该在 ROI 列中,如图所示
  • 编辑:* 不使用熊猫

标签: python python-3.x dictionary printing format


【解决方案1】:

选项 1 - 熊猫

我建议为此使用pandas 数据框:

import pandas as pd
d = {'ffa': {'cope1': 0.6525, 'cope2': 0.4146, 'cope3': 0.5896, 'cope4': 0.1521, 'cope5': 0.5317}, 'lingual': {'cope1': -0.08865060000000001, 'cope2': -0.150985, 'cope3': -0.162005, 'cope4': -0.130845, 'cope5': -0.126411}, 'ppa': {'cope1': 0.74836, 'cope2': 0.9444, 'cope3': 0.300482, 'cope4': 1.12435, 'cope5': 0.8332200000000001}}
data = [(k,*x)  for k, v in d.items() for x in v.items()]
df = pd.DataFrame(data, columns = ['ROI', 'COPE', 'MEAN'])

给予:

>>> df
        ROI   COPE      MEAN
0       ffa  cope1  0.652500
1       ffa  cope2  0.414600
2       ffa  cope3  0.589600
3       ffa  cope4  0.152100
4       ffa  cope5  0.531700
5   lingual  cope1 -0.088651
6   lingual  cope2 -0.150985
7   lingual  cope3 -0.162005
8   lingual  cope4 -0.130845
9   lingual  cope5 -0.126411
10      ppa  cope1  0.748360
11      ppa  cope2  0.944400
12      ppa  cope3  0.300482
13      ppa  cope4  1.124350
14      ppa  cope5  0.833220

选项 2 - 只打印表格

不使用 pandas 只打印:

d = {'ffa': {'cope1': 0.6525, 'cope2': 0.4146, 'cope3': 0.5896, 'cope4': 0.1521, 'cope5': 0.5317}, 'lingual': {'cope1': -0.08865060000000001, 'cope2': -0.150985, 'cope3': -0.162005, 'cope4': -0.130845, 'cope5': -0.126411}, 'ppa': {'cope1': 0.74836, 'cope2': 0.9444, 'cope3': 0.300482, 'cope4': 1.12435, 'cope5': 0.8332200000000001}}
data = [(k,*x)  for k, v in d.items() for x in v.items()]
print ("ROI            COPE        MEAN")
for l in data:
        print ("{:<14}{:<11}{}".format(l[0],l[1],l[2]))

给予:

ROI            COPE        MEAN
ffa           cope1      0.6525
ffa           cope2      0.4146
ffa           cope3      0.5896
ffa           cope4      0.1521
ffa           cope5      0.5317
lingual       cope1      -0.08865060000000001
lingual       cope2      -0.150985
lingual       cope3      -0.162005
lingual       cope4      -0.130845
lingual       cope5      -0.126411
ppa           cope1      0.74836
ppa           cope2      0.9444
ppa           cope3      0.300482
ppa           cope4      1.12435
ppa           cope5      0.8332200000000001

【讨论】:

    【解决方案2】:

    如果我理解正确的话:

    data = {'ffa': {'cope1': 0.6525, 'cope2': 0.4146, 'cope3': 0.5896, 'cope4': 0.1521, 'cope5': 0.5317},
            'lingual': {'cope1': -0.08865060000000001, 'cope2': -0.150985, 'cope3': -0.162005, 'cope4': -0.130845, 'cope5': -0.126411},
            'ppa': {'cope1': 0.74836, 'cope2': 0.9444, 'cope3': 0.300482, 'cope4': 1.12435, 'cope5': 0.8332200000000001}}
    
    def items1(data):
        for roi, v in data.items():
            for cope, mean in v.items():
                yield (roi, cope, mean)
    
    def items2(data):
        return (
            (roi, cope, mean)
                for roi, v in data.items()
                    for cope, mean in v.items()
        )
    
    def pretty_print(data_items, formatter='{:<8} {:<15} {:<10}'.format):
        print(formatter('ROI', 'COPE', 'MEAN'))
        for itm in data_items:
            print(formatter(*itm))
    
    assert list(items1(data)) == list(items2(data))
    
    pretty_print(items2(data))
    

    结果:

    ROI      COPE            MEAN      
    ffa      cope1           0.6525    
    ffa      cope2           0.4146    
    ffa      cope3           0.5896    
    ffa      cope4           0.1521    
    ffa      cope5           0.5317    
    lingual  cope1           -0.08865060000000001
    lingual  cope2           -0.150985 
    lingual  cope3           -0.162005 
    lingual  cope4           -0.130845 
    lingual  cope5           -0.126411 
    ppa      cope1           0.74836   
    ppa      cope2           0.9444    
    ppa      cope3           0.300482  
    ppa      cope4           1.12435   
    ppa      cope5           0.8332200000000001
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2021-08-09
      • 2022-12-05
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多