This question already has an answer here:
这个问题在这里已有答案:
- How can I pretty-print ASCII tables with Python? [closed] 13 answers
- 如何用Python漂亮地打印ASCII表? [已关闭] 13个答案
I have this list of lists:
我有这个列表清单:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
that i have to transform into this table:
我必须转换成这个表:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
The trick for me, is have the "lines" to be converted into columns (i.e. apples, oranges, cherries, banana under same column)
对我来说,诀窍是将“线”转换成列(即同一列下的苹果,橘子,樱桃,香蕉)
I have tried different options (A):
我尝试过不同的选择(A):
for row in tableData:
output = [row[0].ljust(20)]
for col in row[1:]:
output.append(col.rjust(10))
print(' '.join(output))
option (B):
选项(B):
method 2
for i in tableData:
print( i[0].ljust(10)+(str(i[1].ljust(15)))+(str(i[2].ljust(15)))+
(str(i[3].ljust(15))))
None seems to address the issue.
Thanks in advance for any suggestions.
似乎没有人能解决这个问题。在此先感谢您的任何建议。
4 个解决方案
#1
5
To transpose the table, use the zip-and-splat trick.
要转置表格,请使用zip-and-splat技巧。
To left-or-right-justify cells, use the format spec language:
要左对齐或右对齐单元格,请使用格式规范语言:
>>> for row in zip(*tableData):
... print '{:<10}{:>7} {:<10}'.format(*row)
...
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
#2
1
The easiest way to "flip" the nested list is to use zip
:
“翻转”嵌套列表的最简单方法是使用zip:
for fruit, name, animal in zip(*tableData):
print(fruit.ljust(10), name.ljust(10), animal.ljust(10))
This prints:
这打印:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
#3
1
One could also play around with pandas.DataFrame
:
一个人也可以玩pandas.DataFrame:
In [22]: import pandas as pd
In [22]: pd.DataFrame(tableData).T # .T means transpose the dataframe
Out[22]:
0 1 2
0 apples Alice dogs
1 oranges Bob cats
2 cherries Carol moose
3 banana David goose
Remove those annoying numbers by setting columns and indices to blank:
通过将列和索引设置为空来删除那些烦人的数字:
In [27]: l1, l2 = len(tableData), len(tableData[0])
In [28]: pd.DataFrame(tableData, index=['']*l1, columns=['']*l2).T
Out[28]:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
#4
0
There is already a builtin function for this: zip
.
已有内置功能:zip。
zip(* [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']])
#1
5
To transpose the table, use the zip-and-splat trick.
要转置表格,请使用zip-and-splat技巧。
To left-or-right-justify cells, use the format spec language:
要左对齐或右对齐单元格,请使用格式规范语言:
>>> for row in zip(*tableData):
... print '{:<10}{:>7} {:<10}'.format(*row)
...
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
#2
1
The easiest way to "flip" the nested list is to use zip
:
“翻转”嵌套列表的最简单方法是使用zip:
for fruit, name, animal in zip(*tableData):
print(fruit.ljust(10), name.ljust(10), animal.ljust(10))
This prints:
这打印:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
#3
1
One could also play around with pandas.DataFrame
:
一个人也可以玩pandas.DataFrame:
In [22]: import pandas as pd
In [22]: pd.DataFrame(tableData).T # .T means transpose the dataframe
Out[22]:
0 1 2
0 apples Alice dogs
1 oranges Bob cats
2 cherries Carol moose
3 banana David goose
Remove those annoying numbers by setting columns and indices to blank:
通过将列和索引设置为空来删除那些烦人的数字:
In [27]: l1, l2 = len(tableData), len(tableData[0])
In [28]: pd.DataFrame(tableData, index=['']*l1, columns=['']*l2).T
Out[28]:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
#4
0
There is already a builtin function for this: zip
.
已有内置功能:zip。
zip(* [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']])