0

I am quite new to Python and I am now struggling with formatting my data nicely for printed output.

I have three lists:

list1 = [a, b, c, a, b, c, a,  b, c]
list2 = [day1, day2, day3]
list3 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Now, I want to represent this in a table format, something like this:

------------+--------+-------+-------+
|    User    |  day1  | day2  | day3  | 
+============+========+=======+=======+
|a           |  1     |  2    |   3   |
+------------+--------+-------+-------+
| b          | 4      | 5     |   6   |
+----- ------+--- ----+-------+ ------+
|c           |  7     | 8     | 9     |

I have tired different modules like texttable and also tried to use For loop combinations. but still i cannot catch right combination.

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
  • 3
    Post the for-loop attempt in your question. – WillardSolutions Nov 26 '18 at 14:50
  • I would suggest taking a look at Pandas and creating a DataFrame from this lists –  Nov 26 '18 at 14:52
  • Welcome to SO ;) Please read this article on [how to ask a good question](https://stackoverflow.com/help/how-to-ask). This would include a proper description of what you are trying to achieve, your code (or the relevant snippets) as well as your efforts showing what you have tried so far and possible error messages. Is this a homework assignment or shool related? – iLuvLogix Nov 26 '18 at 14:54

2 Answers2

3

I have used prettyTable in the past and it is quite efficient in accomplishing this task. PrettyTable link

Update:

Please see this stackoverflow question for more details as well - How to use PrettyTable

Sample code (copied from the following link) tries to show what it does (although it uses a package that has been forked from PrettyTable, but I believe the usage remains the same).

#!/usr/bin/python3

from prettytable import PrettyTable

x = PrettyTable()

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]

x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])

print(x)

This is a very simple answer. However it does show how easy it is to setup and use it.

Thanks.

Gagan
  • 5,416
  • 13
  • 58
  • 86
1

Astropy has a nice asciitables library that appears to solve this.

http://docs.astropy.org/en/latest/io/ascii/index.html

python-tabulate also appears to try to solve this:

https://bitbucket.org/astanin/python-tabulate

But Pandas is the default python library for tabular data as far as I know. Look how pretty your tables can be in a Jupyter notebook!

enter image description here

To make a dataframe out of your lists you could just do the following with pandas installed:

import pandas a pd
list1 = [a, b, c, a, b, c, a,  b, c]
list2 = [day1, day2, day3]
list3 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

df = pd.DataFrame({
    "list1": list1,
    "list2": list2,
    "list3": list3,
})
Charles Landau
  • 4,187
  • 1
  • 8
  • 24
  • 2
    Please post code as text, not as images. Your answer does not show how to come from OP's input to OP's desired output. – Mike Scotty Nov 26 '18 at 14:59
  • I'm going to keep the image because it's intended to show how aesthetically pleasing Pandas can make your tables but I'll add example code thanks @MikeScotty – Charles Landau Nov 26 '18 at 15:00