TAGS :Viewed: 20 - Published at: a few seconds ago

[ Create a table of permutation data python ]

I have four lists. Two are category lists, one is a list of permutations of the two categories, and the last is a value for each permutation.

For example:

cat1 = [red, green, blue, yellow]
cat2 = [round, square, hard, soft]
permutation = ['red round', 'red square', 'red hard', 'red soft' ....]
values = [8, 10, 9, 14...]

The numbers in values align with permutation. That is to say, the value of 'red round' is 8, 'red square' is 10, etc.

My lists are long and so this information isn't very easily visualized in list form. Is there a way to create a table so that the two categories are the rows and columns and my permutation data fills in the cells?

What I want is:

        red    green    blue   yellow
round    8       

square   10

hard     9           ...

soft     14

I have no idea how to even start this, so I don't have any example code, but any pointers or suggestions to the right direction would be very helpful.

Note: I checked out this: Formatting output as table However, I'm not sure how to adapt it to my situation when I don't have a pre-made list containing all of the information together.

Thank you!

Answer 1


You can do this easily using the pandas library. It will do all the pretty printing for you, you just have to provide the data in an appropriate format:

>>> import numpy as np
>>> import pandas as pd

>>> # Create your data structures
>>> cat1 = ['red', 'green']
>>> cat2 = ['round', 'square']
>>> values = [8, 10, 9, 14]

>>> # Reshape your values to a two dimensional numpy array
>>> data = np.array(values)
>>> data.shape = (len(cat2), len(cat1))

>>> # Create a DataFrame and print it
>>> df = pd.DataFrame(data, columns=cat1, index=cat2)
>>> df
        red  green
round     8     10
square    9     14

or:

>>> print(df.to_string())
        red  green
round     8     10
square    9     14

Answer 2


Here's an approach in which the data gets packed into a 2D list in the format you requested above to prepare it for printing, then use a nice little pretty-printing script that thg435 wrote in another answer to print the 2D list out with even spacing.

cat1 = ['red', 'green', 'blue', 'yellow']
cat2 = ['round', 'square', 'hard', 'soft']

permutations = [c1 + " " + c2 for c1 in cat1 for c2 in cat2]    # making the actual permutation list
values = range(len(permutations))  # creating some values for us to use

# This part puts the permutation values into a list of lists to prepare it for printing:
matrix = [[" "] + cat1]
for c2 in cat2:
    row = [c2]
    for c1 in cat1:
        permutation = c1 + " " + c2
        row.append(values[permutations.index(permutation)])
    matrix.append(row)

#  The printing part is code I lifted (and slightly adapted) from @thg435's answer (see below for link):
s = [[str(e) for e in row] for row in matrix]
lens = [len(max(col, key=len)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
print '\n\n'.join(table)

Here is a link to @thg435's array pretty-printing script answer.