tableData=[['apples','oranges','cherries','banana'],
['Alice','Bob','Carol','David'],
['dogs','cats','moose','goose'] ]
def colwidth(table1):
temp=['0']*len(table1[0])
for col in range(len(table1[0])):
temp[col]=str(len(table1[0][col]))
for row in range(len(table1)):
temp1=table1[row][col]
if int(temp[col])<len(temp1):
temp[col]=len(temp1)
return temp
temp=colwidth(tableData)
for i in range(len(tableData)):
for j in range(len(tableData[0])):
print(tableData[i][j].rjust(int(temp[j])),end=' ')
print()
#打印后的结果
apples oranges cherries banana
Alice Bob Carol David
dogs cats moose goose