简体   繁体   English

垂直格式化表格中的多个列表列表

[英]format multiple list of lists in a table vertically

I am new to formatting and trying to format multiple lists of list to be tidy.我不熟悉格式化并尝试将多个列表列表格式化为整洁。 My code I have tried, which I have used for single list of lists is:我尝试过的用于单个列表列表的代码是:

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']))

The problem I'm having is the rows and columns are reversed.我遇到的问题是行和列颠倒了。

I want for example, Orig to be ['all', 4],['away', 1],['ball', 0],['before', 1]例如,我想 Orig 是 ['all', 4],['away', 1],['ball', 0],['before', 1]

Any help would be appreciated inc.任何帮助将不胜感激公司。 other ideas.其他想法。

Thanks in advance提前致谢

You have to transpose the table.你必须转置表格。 One way to do that is using zip :一种方法是使用zip

print(
    tabulate(
        zip(orig, first, second, third),
        headers=['Orig', 'First', 'Second', 'Third']
    )
)

Result:结果:

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]

Cant you just use dictionaries?你不能只用字典吗?

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.

I hope this helps you, not sure if this will work in your specific situation, though, but you can try.我希望这对你有帮助,虽然不确定这是否适用于你的具体情况,但你可以试试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM