1

I have a list of dictionaries in a form like shown below:

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

Now I want to create a table, where keys will be names of columns and values will be rows. I will add that mylist has 8 elements. I tried it this way:

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)

I get an error: TypeError: unhashable type: 'list'

The thing to note is that sometimes there are more keys in one dictionary than in the other, then I would like the cell to be empty.

Any help appreciated!

ryszard eggink
  • 315
  • 3
  • 14

1 Answers1

3

If I understand your question correctly, this should work for you:

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)

Output:

+---------------------+---+----------------------+---------------------+---------------------+----------------------+----------------------+----------------------+----------------------+---+---------------------+---------------------+----------------------+---------------------+---+---------------------+---------------------+----------------------+---------------------+----------------------+------------------------+----------------------+----------------------+-----------------------+----------------------+---+
|          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 |   |
+---------------------+---+----------------------+---------------------+---------------------+----------------------+----------------------+----------------------+----------------------+---+---------------------+---------------------+----------------------+---------------------+---+---------------------+---------------------+----------------------+---------------------+----------------------+------------------------+----------------------+----------------------+-----------------------+----------------------+---+
Ed Ward
  • 2,333
  • 2
  • 10
  • 16
  • Thank you, this is very helpful. I have one question, if now I wanted to add another column, but wanted to make it first column, how could I do that? The function add column makes it last and if I add it first, then I get an error in line x.add_column(c,[]) that column length does not match number of rows – ryszard eggink Mar 07 '20 at 13:04
  • Do you have all the required column names at the start of the program, or do you need to add a new column after inserting some of the data? Basically, is it at all possible to create all the columns at the start? – Ed Ward Mar 07 '20 at 20:51
  • @EdWard Is there a way to pass the direction of the print ?, like from horizantal to vertical? – Esteban G. Gutierrez Dec 23 '21 at 20:17
  • If you mean printing the characters sideways, then no, however, if you mean printing the table with headers on the left rather than the top, then yes. I can't write any code at the moment, but you could start by trying to making the list be column first, rather than row first. I would suggest creating a list of lists for each column, and looping through each row, adding its contents to the lists. This list of lists is now your new data. I hope this makes some sense, and will do what you want. – Ed Ward Dec 24 '21 at 21:10