skip to main |
skip to sidebar
#! /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)
没有评论:
发表评论