在python制表中创建嵌套表

时间:2015-09-01 12:29:48

标签: python-3.x latex

我想在python中创建一个LaTeX表:

TextView that extends RelativeLayout

我该怎么做?

我的名单如下:

duck    small    dog    small
        medium          medium
        large           large

2 个答案:

答案 0 :(得分:0)

我想出来了。

这个想法不是将其视为嵌套表,而是将其视为一个表。在这种情况下,一个包含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
---  ------  ----  ------

答案 1 :(得分:0)

您可以简单地将第二列与换行符组合成一个长字符串:

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       |
+----------+--------------+