新手上路,首次分享。
考虑到了不规则列表,将原tableData稍作了改动。
tableData = [['apples', 'oranges', 'cherries', 'banana', 'test1'],
['Alice', 'Bob', ' ','Carol', 'David'],
['dogs', 'cats', 'moose', 'goose'],
['test2']]
def printTable(table):
x = len(table)
y = len(table[0])
newTable = [
len(s)
for i in table
for s in i
]
width = max(newTable)
for j in range(y):
for i in range(x):
try:
print(table[i][j].rjust(width), end='')
except IndexError:
pass
print()
printTable(tableData)
运行结果如下:
apples Alice dogs test2
oranges Bob cats
cherries moose
banana Carol goose
test1 David