垂直格式化 table 中的多个列表列表
format multiple list of lists in a table vertically
我是格式化的新手,正在尝试将多个列表列表格式化为整洁。我尝试过的用于单个列表列表的代码是:
from tabulate import tabulate
orig = [['all', 4], ['away', 1], ['ball', 0], ['before', 1]]
first = [['every', 5], ['home', 1], ['game', 2], ['time', 2]]
second = [['one', 7], ['family', 1], ['sport', 3], ['now', 3]]
third = [['day', 10], ['friends', 1], ['game', 8], ['never', 3]]
print(tabulate([orig, first, second, third], headers=['Orig', 'First', 'Second', 'Third']))
我遇到的问题是行和列颠倒了。
例如,我希望 Orig 为 ['all', 4],['away', 1],['ball', 0],['before', 1]
如有任何帮助,我们将不胜感激。其他想法。
提前致谢
你不能只用字典吗?
orig = {"all": 4, "away": 1, "ball": 0, "before": 1}
# Keep in mind that to get the values you use the key. so:
# orig["all"] would give you 4
# iterating is also different
for key in orig:
print(key, orig[key])
# this will give you the key and the value.
我希望这对你有帮助,虽然不确定这是否适用于你的具体情况,但你可以试试。
您必须转置 table。一种方法是使用 zip
:
print(
tabulate(
zip(orig, first, second, third),
headers=['Orig', 'First', 'Second', 'Third']
)
)
结果:
Orig First Second Third
------------- ------------ ------------- --------------
['all', 4] ['every', 5] ['one', 7] ['day', 10]
['away', 1] ['home', 1] ['family', 1] ['friends', 1]
['ball', 0] ['game', 2] ['sport', 3] ['game', 8]
['before', 1] ['time', 2] ['now', 3] ['never', 3]
我是格式化的新手,正在尝试将多个列表列表格式化为整洁。我尝试过的用于单个列表列表的代码是:
from tabulate import tabulate
orig = [['all', 4], ['away', 1], ['ball', 0], ['before', 1]]
first = [['every', 5], ['home', 1], ['game', 2], ['time', 2]]
second = [['one', 7], ['family', 1], ['sport', 3], ['now', 3]]
third = [['day', 10], ['friends', 1], ['game', 8], ['never', 3]]
print(tabulate([orig, first, second, third], headers=['Orig', 'First', 'Second', 'Third']))
我遇到的问题是行和列颠倒了。
例如,我希望 Orig 为 ['all', 4],['away', 1],['ball', 0],['before', 1]
如有任何帮助,我们将不胜感激。其他想法。
提前致谢
你不能只用字典吗?
orig = {"all": 4, "away": 1, "ball": 0, "before": 1}
# Keep in mind that to get the values you use the key. so:
# orig["all"] would give you 4
# iterating is also different
for key in orig:
print(key, orig[key])
# this will give you the key and the value.
我希望这对你有帮助,虽然不确定这是否适用于你的具体情况,但你可以试试。
您必须转置 table。一种方法是使用 zip
:
print(
tabulate(
zip(orig, first, second, third),
headers=['Orig', 'First', 'Second', 'Third']
)
)
结果:
Orig First Second Third
------------- ------------ ------------- --------------
['all', 4] ['every', 5] ['one', 7] ['day', 10]
['away', 1] ['home', 1] ['family', 1] ['friends', 1]
['ball', 0] ['game', 2] ['sport', 3] ['game', 8]
['before', 1] ['time', 2] ['now', 3] ['never', 3]