我列出這個清單:轉換列表列表變爲表
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 –