从字典列表中创建一个带有漂亮表的表

Posted

技术标签:

【中文标题】从字典列表中创建一个带有漂亮表的表【英文标题】:Creating a table with prettytable from list of dictionaries 【发布时间】:2020-06-19 23:21:53 【问题描述】:

我有一个字典列表,格式如下:

mylist=['M': 0.02788376633371408, 'G': 0.06911804019734125, 'N': 0.03952819565622443, 'K': 0.07071475441708995, 'Q': 0.03835921614374478, 'P': 0.03663450555326026, 'Y': 0.0349154656880615, 'A': 0.07679377192342901, 'S': 0.0628024724362246, 'H': 0.022690192075566464, 'L': 0.09661538710801111, 'T': 0.05419593165965927, 'V': 0.06747352986446967, 'E': 0.07257313210359605, 'D': 0.05184258066606719, 'R': 0.040929674905420405, 'I': 0.07369188519211607, 'C': 0.007873397007477256, 'F': 0.04502555876896655, 'W': 0.010337732195947861, 'X': 8.101036122520071e-07,'M': 0.026012299035369776, 'S': 0.08222942122186495, 'Q': 0.04284397106109325, 'L': 0.0842411575562701, 'T': 0.05967950160771704, 'A': 0.06484501607717041, 'E': 0.06722242765273312, 'D': 0.05424099678456592, 'K': 0.06239324758842444, 'C': 0.01983110932475884, 'G': 0.054260932475884247, 'V': 0.062292363344051443, 'I': 0.059238424437299035, 'F': 0.04464646302250804, 'N': 0.04793086816720257, 'Y': 0.030552572347266882, 'W': 0.010734163987138263, 'R': 0.05250265273311897, 'H': 0.02322789389067524, 'P': 0.05107435691318328, 'U': 1.6077170418006432e-07,'M': 0.025028481439857867, 'C': 0.018105322240839924, 'K': 0.0623929624244052, 'A': 0.06482217306201797, 'E': 0.06590914284008073, 'F': 0.043525892211680704, 'L': 0.09655372825447149, 'Y': 0.029081277835727382, 'H': 0.022226947906896288, 'R': 0.0528779816846854, 'W': 0.012560430627315015, 'V': 0.06773431004544274, 'G': 0.06567321684878855, 'D': 0.05337478602998517, 'S': 0.0884704243710007, 'I': 0.054255127815674375, 'N': 0.04415657018840295, 'T': 0.051248648472095336, 'P': 0.04731066098107924, 'Q': 0.03469191471955296]

现在我想创建一个表,其中键是列名,值是行。我将添加 mylist 有 8 个元素。我是这样尝试的:

column_names=[mylist[0].keys()]

for i in column_names:
    for k in range(0,8):
        x.add_column(column_names,mylist[k][column_names])
print(x)

我收到一个错误:TypeError: unhashable type: 'list'

需要注意的是,有时一个字典中的键比另一个字典中的键多,那么我希望单元格为空。

任何帮助表示赞赏!

【问题讨论】:

【参考方案1】:

如果我正确理解您的问题,这应该适合您:

from prettytable import PrettyTable
import string

mylist=['M': 0.02788376633371408, 'G': 0.06911804019734125, 'N': 0.03952819565622443, 'K': 0.07071475441708995, 'Q': 0.03835921614374478, 'P': 0.03663450555326026, 'Y': 0.0349154656880615, 'A': 0.07679377192342901, 'S': 0.0628024724362246, 'H': 0.022690192075566464, 'L': 0.09661538710801111, 'T': 0.05419593165965927, 'V': 0.06747352986446967, 'E': 0.07257313210359605, 'D': 0.05184258066606719, 'R': 0.040929674905420405, 'I': 0.07369188519211607, 'C': 0.007873397007477256, 'F': 0.04502555876896655, 'W': 0.010337732195947861, 'X': 8.101036122520071e-07,'M': 0.026012299035369776, 'S': 0.08222942122186495, 'Q': 0.04284397106109325, 'L': 0.0842411575562701, 'T': 0.05967950160771704, 'A': 0.06484501607717041, 'E': 0.06722242765273312, 'D': 0.05424099678456592, 'K': 0.06239324758842444, 'C': 0.01983110932475884, 'G': 0.054260932475884247, 'V': 0.062292363344051443, 'I': 0.059238424437299035, 'F': 0.04464646302250804, 'N': 0.04793086816720257, 'Y': 0.030552572347266882, 'W': 0.010734163987138263, 'R': 0.05250265273311897, 'H': 0.02322789389067524, 'P': 0.05107435691318328, 'U': 1.6077170418006432e-07,'M': 0.025028481439857867, 'C': 0.018105322240839924, 'K': 0.0623929624244052, 'A': 0.06482217306201797, 'E': 0.06590914284008073, 'F': 0.043525892211680704, 'L': 0.09655372825447149, 'Y': 0.029081277835727382, 'H': 0.022226947906896288, 'R': 0.0528779816846854, 'W': 0.012560430627315015, 'V': 0.06773431004544274, 'G': 0.06567321684878855, 'D': 0.05337478602998517, 'S': 0.0884704243710007, 'I': 0.054255127815674375, 'N': 0.04415657018840295, 'T': 0.051248648472095336, 'P': 0.04731066098107924, 'Q': 0.03469191471955296]

table = PrettyTable()

for c in string.ascii_uppercase:
    table.add_column(c, [])

for dct in mylist:
    table.add_row([dct.get(c, "") for c in string.ascii_uppercase])

print(table)

输出:

+---------------------+---+----------------------+---------------------+---------------------+----------------------+----------------------+----------------------+----------------------+---+---------------------+---------------------+----------------------+---------------------+---+---------------------+---------------------+----------------------+---------------------+----------------------+------------------------+----------------------+----------------------+-----------------------+----------------------+---+
|          A          | B |          C           |          D          |          E          |          F           |          G           |          H           |          I           | J |          K          |          L          |          M           |          N          | O |          P          |          Q          |          R           |          S          |          T           |           U            |          V           |          W           |           X           |          Y           | Z |
+---------------------+---+----------------------+---------------------+---------------------+----------------------+----------------------+----------------------+----------------------+---+---------------------+---------------------+----------------------+---------------------+---+---------------------+---------------------+----------------------+---------------------+----------------------+------------------------+----------------------+----------------------+-----------------------+----------------------+---+
| 0.07679377192342901 |   | 0.007873397007477256 | 0.05184258066606719 | 0.07257313210359605 | 0.04502555876896655  | 0.06911804019734125  | 0.022690192075566464 | 0.07369188519211607  |   | 0.07071475441708995 | 0.09661538710801111 | 0.02788376633371408  | 0.03952819565622443 |   | 0.03663450555326026 | 0.03835921614374478 | 0.040929674905420405 |  0.0628024724362246 | 0.05419593165965927  |                        | 0.06747352986446967  | 0.010337732195947861 | 8.101036122520071e-07 |  0.0349154656880615  |   |
| 0.06484501607717041 |   | 0.01983110932475884  | 0.05424099678456592 | 0.06722242765273312 | 0.04464646302250804  | 0.054260932475884247 | 0.02322789389067524  | 0.059238424437299035 |   | 0.06239324758842444 |  0.0842411575562701 | 0.026012299035369776 | 0.04793086816720257 |   | 0.05107435691318328 | 0.04284397106109325 | 0.05250265273311897  | 0.08222942122186495 | 0.05967950160771704  | 1.6077170418006432e-07 | 0.062292363344051443 | 0.010734163987138263 |                       | 0.030552572347266882 |   |
| 0.06482217306201797 |   | 0.018105322240839924 | 0.05337478602998517 | 0.06590914284008073 | 0.043525892211680704 | 0.06567321684878855  | 0.022226947906896288 | 0.054255127815674375 |   |  0.0623929624244052 | 0.09655372825447149 | 0.025028481439857867 | 0.04415657018840295 |   | 0.04731066098107924 | 0.03469191471955296 |  0.0528779816846854  |  0.0884704243710007 | 0.051248648472095336 |                        | 0.06773431004544274  | 0.012560430627315015 |                       | 0.029081277835727382 |   |
+---------------------+---+----------------------+---------------------+---------------------+----------------------+----------------------+----------------------+----------------------+---+---------------------+---------------------+----------------------+---------------------+---+---------------------+---------------------+----------------------+---------------------+----------------------+------------------------+----------------------+----------------------+-----------------------+----------------------+---+

【讨论】:

谢谢,这很有帮助。我有一个问题,如果现在我想添加另一列,但又想将其设为第一列,我该怎么做?函数添加列使它最后,如果我先添加它,那么我在行 x.add_column(c,[]) 中得到一个错误,即列长度与行数不匹配 你在程序开始时有所有需要的列名,还是需要在插入一些数据后添加一个新列?基本上,是否有可能在开始时创建所有列? @EdWard 有没有办法传递打印方向?比如从水平到垂直? 如果您的意思是横向打印字符,那么不是,但是,如果您的意思是打印表格的标题在左侧而不是顶部,那么是的。我目前无法编写任何代码,但您可以先尝试使列表成为列优先,而不是行优先。我建议为每一列创建一个列表列表,并遍历每一行,将其内容添加到列表中。此列表列表现在是您的新数据。我希望这是有道理的,并且会做你想做的事。

以上是关于从字典列表中创建一个带有漂亮表的表的主要内容,如果未能解决你的问题,请参考以下文章

从给定列表中创建一个包含所有可能组合的表,其中包含两列(excel)

一个班轮:从列表中创建一个字典,索引为键

如何从具有不同长度列表的字典中创建字典列表

如何从字典列表中创建数据框 [关闭]

使用 Counter 对象从两个列表中创建字典

我需要从包含列表的字典中使用 MultiIndex 在 Pandas 中创建一个 DataFrame