在 python 制表中创建嵌套表格
creating nested tables in python tabulate
我想创建一个 LaTeX table 如下 python:
duck small dog small
medium medium
large large
我该怎么做?
我的列表如下:
lis=['dog',['small','medium','large],'duck',['small','medium','large']]
我想通了。
我们的想法不是将其视为嵌套 table,而是将其视为一个 table。在本例中,table 有 4 列,因此您将列表重新格式化为:
lis=[('dog','small','duck','small'),('','medium','','medium'),('','large','','large)]
然后使用制表包:
from tabulate import tabulate
print(tabulate(lis))
瞧:
--- ------ ---- ------
dog small duck small
medium medium
large large
--- ------ ---- ------
您可以简单地将第二列合并为一个带有换行符的长字符串:
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"))
输出:
+----------+--------------+
| animal | categories |
+==========+==============+
| dog | small |
| | medium |
| | large |
+----------+--------------+
| fish | wet |
| | dry |
| | happy |
| | sad |
+----------+--------------+
| kangaroo | depressed |
| | joyful |
+----------+--------------+
我想创建一个 LaTeX table 如下 python:
duck small dog small
medium medium
large large
我该怎么做?
我的列表如下:
lis=['dog',['small','medium','large],'duck',['small','medium','large']]
我想通了。
我们的想法不是将其视为嵌套 table,而是将其视为一个 table。在本例中,table 有 4 列,因此您将列表重新格式化为:
lis=[('dog','small','duck','small'),('','medium','','medium'),('','large','','large)]
然后使用制表包:
from tabulate import tabulate
print(tabulate(lis))
瞧:
--- ------ ---- ------
dog small duck small
medium medium
large large
--- ------ ---- ------
您可以简单地将第二列合并为一个带有换行符的长字符串:
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"))
输出:
+----------+--------------+
| animal | categories |
+==========+==============+
| dog | small |
| | medium |
| | large |
+----------+--------------+
| fish | wet |
| | dry |
| | happy |
| | sad |
+----------+--------------+
| kangaroo | depressed |
| | joyful |
+----------+--------------+