def printTable(table):
colWidths = [0] * len(table)
lines = len(table) # 3行
cols = len(table[0]) # 4列
for i in range(lines): # 遍历列表3行
col_max_len = 0
for j in range(cols): # 遍历列表4列
if len(table[i][j]) > col_max_len:
col_max_len = len(table[i][j])
colWidths[i] = col_max_len
for j in range(cols): # 输出4行
for i in range(lines): # 输出3列
print(table[i][j].rjust(colWidths[i]), end=' ')
print('')
tableData = [['apple', 'orange', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
printTable(tableData)
6.7 表格打印
最新推荐文章于 2022-04-22 15:01:50 发布