Reputation: 1059
I want to create a LaTeX table as follows in python:
duck small dog small
medium medium
large large
how would I do that?
The list I have looks like:
lis=['dog',['small','medium','large],'duck',['small','medium','large']]
Upvotes: 3
Views: 5954
Reputation: 1059
You can simply combine the second column into one long string with newlines:
from tabulate import tabulate
table = []
table.append(["dog", "\n".join(["small", "medium", "large"])])
table.append(["fish", "\n".join(["wet", "dry", "happy", "sad"])])
table.append(["kangaroo", "\n".join(["depressed", "joyful"])])
print(tabulate(table, ["animal", "categories"], "grid"))
output:
+----------+--------------+
| animal | categories |
+==========+==============+
| dog | small |
| | medium |
| | large |
+----------+--------------+
| fish | wet |
| | dry |
| | happy |
| | sad |
+----------+--------------+
| kangaroo | depressed |
| | joyful |
+----------+--------------+
Upvotes: 1
Reputation: 1059
I figured this out.
The idea is not to treat this as nested tables but as one table. In this case a table with 4 columns, so you reformat your list as:
lis=[('dog','small','duck','small'),('','medium','','medium'),('','large','','large)]
then use the tabulate package:
from tabulate import tabulate
print(tabulate(lis))
voila:
--- ------ ---- ------
dog small duck small
medium medium
large large
--- ------ ---- ------
Upvotes: 1