Python编程快速上手之第6.7实践
#! /user/local/bin/python3#print the tabletableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']]
# Code for printing table
def printTable(data):
maxRow = len(max(data)) # determine the row of table
numberOfColumn = len(data) # determine the cloumn of table
colWidths=[0]*numberOfColumn # find the widest word in each column
for i in range(numberOfColumn):
colWidths[i]=len(max(data[i]))
maxWidth = max(colWidths) # determine the width
#以下需要优化
# print the table
for j in range(maxRow):
for i in range(numberOfColumn):
print(data[i][j].ljust(maxWidth+5), end='') # print the first item of the each sublist
print()
printTable(tableData)