#表格打印: tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']] def printTable(): #计算列表每行最大字符长度的集合 colWidths = [] for i in tableData: count = [] for j in i: count.append(len(j)) colWidths.append(max(count)) print("每个字列表的最大字符长度集合为:",end="") print(colWidths) print("表格格式化打印如下:") for i in range(len(tableData[0])): k=0 for j in range(len(tableData)): if j == 0: print(tableData[j][i].rjust(colWidths[k], " "), end=" ") else: print(tableData[j][i].ljust(colWidths[k], " "), end=" ") k+=1 print() printTable()
第6章 字符串操作之表格打印
最新推荐文章于 2023-09-11 10:21:03 发布