下面是给出的实现代码
#定义列表,用来保存每个内层列表(行)中的最长字符串长度,这样整列就有足够的宽度放下所有字符串了。
colWidth = [0] * len(tableData)//>>> colWidth//[0, 0, 0]
#定义每个内层列表(行)的宽度,用来遍历计算和打印。
rowWidth = len(tableData[0])//>>> rowWidth//4
#打印出tableData的行列宽度信息
print("The colWidth is "+str(len(colWidth))+"\nTheRowWidth is "+str(rowWidth))//The colWidth is 3//The RowWidth is 4
#找出每行中最长的字符串宽度,然后保存在colWidth[i]中
for i in range(len(colWidth)): com = 0 for k in range(rowWidth): iflen(tableData[i][k]) > com: com =len(tableData[i][k]) colWidth[i] = com //>>> colWidth//[8, 5, 5]
#按照格式使用.rjust()打印tableData
for i in range(rowWidth): for k inrange(len(colWidth)): print(tableData[k][i].rjust(colWidth[k]),end=' ') print('')打印结果:
apples Alice dogs oranges Bob cats cherries Carol moose banana David goose