编写一个名为 printTable()的函数,它接受字符串的列表的列表,将它显示在组织良好的表格中,每列右对齐。假定所有内层列表都包含同样数目的字符串。例如,
该值可能看起来像这样:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

 

# -*- coding:utf-8 -*-

def printTable(lists):
    len_list = []
    for i in range(len(lists)):
        temp = 0
        for j in range(len(lists[i])):
            if len(lists[i][j]) > temp:
                temp = len(lists[i][j])
        len_list.append(temp)

    for i in range(len(lists[0])):
        for j in range(len(lists)):
            print(lists[j][i].rjust(len_list[j]) + '\t', end='')
            temp = len_list[j]
        print()

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

printTable(tableData)

 

 

结果:

每日练习四:《Python编程快速上手+让繁琐工作自动化》第六章实践项目

 

相关文章:

  • 2021-05-27
  • 2021-09-02
  • 2022-12-23
  • 2021-11-29
  • 2021-12-05
  • 2021-12-11
  • 2021-10-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2021-12-05
  • 2021-08-06
相关资源
相似解决方案