Python printing lists with tabulate

10,811

Solution 1

Use zip() like this:

amp = ['a1', 'a2', 'a3', 'a4']
mass = ['m1', 'm2', 'm3', 'm4']
period = ['p1', 'p2', 'p3', 'p4']
ecc = ['e1', 'e2', 'e3', 'e4']
planet = [1, 2, 3, 4]


titles = ['planet', 'amp', 'mass', 'period', 'ecc']

print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*titles)
for item in zip(planet, amp, mass, period, ecc):
    print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*item)

Instead of using planet = [1, 2, 3, 4], you can use enumerate() like below:

print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*titles)
for i, item in enumerate(zip(amp, mass, period, ecc)):
    print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(i+1, *item)

Output:

>>> python print_table.py
planet|amp   |mass  |period|ecc
1     |a1    |m1    |p1    |e1
2     |a2    |m2    |p2    |e2
3     |a3    |m3    |p3    |e3
4     |a4    |m4    |p4    |e4

Solution 2

You can combine the usage of zip() with tabulate to create a nicer looking table:

from tabulate import tabulate
headers = ['planet', 'amp', 'mass', 'period', 'ecc']    

amp = [1.1, 1.2, 1.3, 1.4]
mass = [2.1, 2.2, 2.3, 2.4]
period = [3.1, 3.2, 3.3, 3.4]
ecc = [4.1, 4.2, 4.3, 4.4]
planet = range(1, len(amp)+1)

table = zip(planet, amp, mass, period, ecc)
print(tabulate(table, headers=headers, floatfmt=".4f"))

Output:

  planet     amp    mass    period     ecc
--------  ------  ------  --------  ------
       1  1.1000  2.1000    3.1000  4.1000
       2  1.2000  2.2000    3.2000  4.2000
       3  1.3000  2.3000    3.3000  4.3000
       4  1.4000  2.4000    3.4000  4.4000
Share:
10,811
rh1990
Author by

rh1990

Updated on June 12, 2022

Comments

  • rh1990
    rh1990 almost 2 years

    I'm trying to print the output of an astronomy simulation so that it looks nice in my console. I generate 4 numpy arrays called Amplitude, Mass, Period and Eccentricity and I want to put them in a table. The first index of each array are the values for planet 1, the second for planet 2 etc.

    So my arrays look like (values are all float numbers, eg 'a1' just a placeholder):

    amp = [a1 a2 a3 a4]
    mass = [m1 m2 m3 m4]
    period = [p1 p2 p3 p4]
    ecc = [e1 e2 e3 e4]
    

    I'd like my table to look like:

    planet|amp|mass|period|ecc
    1     |a1 |m1  |p1    |e1
    2     |a2 |m2  |p2    |e2
    ...
    

    I've tried using tabulate and something like:

    print tabulate(['1', amp[0], mass[0], period[0], ecc[0]], headers=[...])
    

    but I get an error of 'numpy.float64' object is not iterable

    Any help would be appreciated!

  • TigerhawkT3
    TigerhawkT3 over 7 years
    This does not answer the question and would be better suited as a comment. You will be able to comment once you have sufficient reputation.
  • rh1990
    rh1990 over 7 years
    Thanks for the suggestion. This prints out the column titles in 'titles' but then nothing else underneath. I might have to redefine my arrays like you did...
  • ettanany
    ettanany over 7 years
    @RichardHall You need to run the for loop as well. You can also put the above code in a file like I did with print_table.py
  • rh1990
    rh1990 over 7 years
    The enumerate worked! Thanks! Follow up question, can I format it so that each 'cell' of the table is the same length/size?
  • ettanany
    ettanany over 7 years
    Sure, that's what I have done with :<6. I am using 6 because the longest word is 6 characters (planet and period)
  • rh1990
    rh1990 over 7 years
    Yeah I thought that was controlling it, but the values (eg 'a1') are showing with up 11 decimal places, but others are whole numbers so only show one decimal place ...
  • FelixSFD
    FelixSFD over 7 years
    @TigerhawkT3 No. This is an answer to the question.
  • TigerhawkT3
    TigerhawkT3 over 7 years
    @FelixSFD - No, it isn't an answer to the question. The question is already asking how to tabulate their arrays. This answer is more like a guess regarding the meaning of "tabulate." That doesn't provide an answer or help in any way.
  • rh1990
    rh1990 over 7 years
    I've slightly fixed it, I used '{:0.2f}\t' in all the cells and now they're the same length. Thanks for the help though :)
  • Emjey
    Emjey about 4 years
    Such a simple implementation ! Thanks dude