问题
I am trying to print several lists (equal length) as columns of an table.
I am reading data from a .txt file, and at the end of the code, I have 5 lists, which I would like to print as columns separated but space.
回答1:
I'll show you a 3-list analog:
>>> l1 = ['a', 'b', 'c']
>>> l2 = ['1', '2', '3']
>>> l3 = ['x', 'y', 'z']
>>> for row in zip(l1, l2, l3):
... print ' '.join(row)
a 1 x
b 2 y
c 3 z
回答2:
You can use my package beautifultable . It supports adding data by rows or columns or even mixing both the approaches. You can insert, remove, update any row or column.
Usage
>>> from beautifultable import BeautifulTable
>>> table = BeautifulTable()
>>> table.column_headers = ["name", "rank", "gender"]
>>> table.append_row(["Jacob", 1, "boy"])
>>> table.append_row(["Isabella", 1, "girl"])
>>> table.append_row(["Ethan", 2, "boy"])
>>> table.append_row(["Sophia", 2, "girl"])
>>> table.append_row(["Michael", 3, "boy"])
>>> print(table)
+----------+------+--------+
| name | rank | gender |
+----------+------+--------+
| Jacob | 1 | boy |
+----------+------+--------+
| Isabella | 1 | girl |
+----------+------+--------+
| Ethan | 2 | boy |
+----------+------+--------+
| Sophia | 2 | girl |
+----------+------+--------+
| Michael | 3 | boy |
+----------+------+--------+
Have fun
回答3:
Assming that you have a lists of lists:
for L in list_of_lists:
print " ".join(L)
The str.join(iterable)
function, joins the components of an iterable by the string given.
Therefore, " ".join([1, 2, 3])
becomes "1 2 3".
In case I might have misunderstood the question and each list
is supposed to be a column:
for T in zip(list1, list2, list3, list4, list5):
print " ".join(T)
zip()
merges the given lists to one list of tuples:
>>> zip([1,2,3], [4,5,6], [7,8,9])
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Cheers!
回答4:
for nested_list in big_container_list
print '\t'.join(nested_list)
with \t
being the tabulation character
quick example:
In [1]: a = [['1','2'],['3','4']]
In [5]: for nested_list in a:
...: print '\t'.join(nested_list)
...:
1 2
3 4
回答5:
Try out my library
NOTE: This answer is already posted on this question, and this question.
I just made a library for this that I think could really help. It is extremely simple, that's why I think you should use it. It is called TableIT.
To use it, first follow the download instructions on the Github Page.
Then import it:
import TableIt
Then make a list of lists where each inner list is a row:
table = [
[4, 3, "Hi"],
[2, 1, 808890312093],
[5, "Hi", "Bye"]
]
Then all you have to do is print it:
TableIt.printTable(table, useFieldNames=False)
This is the output you get:
+--------------------------------------------+
| 4 | 3 | Hi |
| 2 | 1 | 808890312093 |
| 5 | Hi | Bye |
+--------------------------------------------+
You can use field names if you want to:
TableIt.printTable(table, useFieldNames=True)
From that you will get:
+--------------------------------------------+
| 4 | 3 | Hi |
+--------------+--------------+--------------+
| 2 | 1 | 808890312093 |
| 5 | Hi | Bye |
+--------------------------------------------+
There are other uses to, for example you could do this:
import TableIt
myList = [
["Name", "Email"],
["Richard", "richard@fakeemail.com"],
["Tasha", "tash@fakeemail.com"]
]
TableIt.print(myList, useFieldNames=True)
From that:
+-----------------------------------------------+
| Name | Email |
+-----------------------+-----------------------+
| Richard | richard@fakeemail.com |
| Tasha | tash@fakeemail.com |
+-----------------------------------------------+
Or you could do:
import TableIt
myList = [
["", "a", "b"],
["x", "a + x", "a + b"],
["z" "a + z", "z + b"]
]
TableIt.printTable(myList, useFieldNames=True)
And from that you get:
+-----------------------+
| | a | b |
+-------+-------+-------+
| x | a + x | a + b |
| z | a + z | z + b |
+-----------------------+
For more information check the Github Page
来源:https://stackoverflow.com/questions/17279059/print-list-in-table-format-in-python