我列出这个清单:转换列表列表变为表
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
,我要改造成这个表:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
我的诀窍,就是有“线“被转换成列(即苹果,橙子,樱桃,下同一列香蕉)
我曾尝试不同的选项(A):
for row in tableData:
output = [row[0].ljust(20)]
for col in row[1:]:
output.append(col.rjust(10))
print(' '.join(output))
选项(B):
方法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))))
没有一个似乎解决这个问题。
在此先感谢您的任何建议。
要说清楚的是,这是Py2还是Py3? 'print'不同于另一个,并且您的示例代码使用它的方式没有行使任何差异来说明问题。 – ShadowRanger
另请参见:[Python:打印列表为表格数据](http://stackoverflow.com/q/9535954/216074) – poke
这是python 3.5 –