python快速编程--打印表格

编写一个名为printTable()函数 接收字符串的列表的列表 将它显示在组织良好的表格中,每列右对齐。假定所有内层列表都包含同样数目的字符串。例如,可能看起来像

tableDate=[['apples', 'oranges', 'cherries', 'banana'],
           ['Alice', 'Bob', 'Carol', 'David'],
           ['dogs', 'cats', 'moose', 'goose']]

你的函数将打印出

       apples     Alice      dogs
      oranges       Bob      cats
     cherries     Carol     moose
       banana     David     goose
#! /usr/bin/python3

tableDate=[['apples', 'oranges', 'cherries', 'banana'],
           ['Alice', 'Bob', 'Carol', 'David'],
           ['dogs', 'cats', 'moose', 'goose']]

def maxStr(Str):
    maxLen = 0
    for i in range(len(Str)):
        if len(Str[i]) > maxLen:
           maxLen = len(Str[i])
        else:
           pass
    return maxLen

def printTable(tableData):
    for column in range(len(tableData[0])):
        for row in range(len(tableDate)):
            print(tableData[row][column].rjust(maxStr(tableData[row])+5),end='')
        print()



printTable(tableDate)


apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose

猜你喜欢

转载自blog.csdn.net/ichglauben/article/details/81589124
今日推荐