1. 程式人生 > >Python程式設計快速上手 專案6.7

Python程式設計快速上手 專案6.7

tableData = [['apple', 'oaras', 'sadfa', 'ajsdasdas'],
             ['asd', 'asdasd', 'asdas', 'asdafw3'],
             ['asda', 'asdasd', 'asdfgr', 'hgtrtty']

             ]


def printTable(tableData):
    colWidths = [0] * len(tableData)

    widths = max(len(tableData[i]) for i in range(len(tableData)))

    for i in range(len(tableData)):
        colWidths[i] = max(len(tableData[i][j]) for j in range(len(tableData[i])))

    for j in range(widths):
        for i in range(len(tableData)):
            try:
                print(tableData[i][j].rjust(colWidths[i]), end=' ')
            except IndexError:
                break
        print()


printTable(tableData)