python print【format】【打印table】【组装list】

  1. 基础知识
# 例子1
import numpy as np

teams_list = ["Man Utd","Man City","T Hotspur"]
data = np.array([[1, 2, 1],
                 [0, 1, 0],
                 [2, 4, 2]])

row_format ="{:>15}" * (len(teams_list) + 1)
print(row_format.format("", *teams_list))
for team, row in zip(teams_list, data):
    print(row_format.format(team, *row))



# 例子2
popularity = [["Language", 2017, 2012, 2007, 2002, 1997, 1992, 1987],
          ["Java", 1, 2, 1, 1, 15, 0, 0],
          ["C", 2, 1, 2, 2, 1, 1, 1],
          ["C++", 3, 3, 3, 3, 2, 2, 5],
          ["C#", 4, 4, 7, 13, 0, 0, 0],
          ["Python", 5, 7, 6, 11, 27, 0, 0],
          ["Visual Basic .NET", 6, 17, 0, 0, 0, 0, 0],
          ["PHP", 7, 6, 4, 5, 0, 0, 0],
          ["JavaScript", 8, 9, 8, 7, 23, 0, 0],
          ["Perl", 9, 8, 5, 4, 4, 10, 0]]

format_string = "{:<20}  {:>4}  {:>4}  {:>4}  {:>4}  {:>4}  {:>4}  {:>4}"

for i in popularity:
    # print(format_string.format("", *i))  # 第一列为空
    print(format_string.format(*i))


  1. 函数化【zip】【列表】【'{:^{}}'】应用
1. zip 打包【可迭代的对象的每个元素】成【元祖】返回【元祖的列表】
# 处理两个列表的方法table_forma,table
zip(zip(*table_format), zip(*table))

# 列的宽度,两个for
col_widths = [max(len(format.format(cell, 0)) for format, cell in zip(col_format, col)) for col_format, col in zip(zip(*table_format), zip(*table))]

# 下面等价
print('{:^{}}'.format('test',10))
print('{:^10}'.format('test'))



# 原版
def format_matrix(header, matrix,
                  top_format, left_format, cell_format, row_delim, col_delim):
    table = [[''] + header] + \
            [[name] + row for name, row in zip(header, matrix)]

    print(table)
    table_format = [['{:^{}}'] + len(header) * [top_format]] +\
                   len(matrix) * [[left_format] + len(header) * [cell_format]]

    print(table_format)
    col_widths = [max(len(format.format(cell, 0)) for format, cell in zip(col_format, col))
                  for col_format, col in zip(zip(*table_format), zip(*table))]
    # 下面实现也行的
    # col_widths = [max(len(format.format(cell, 0)) for format, cell in zip(col_format, col))
    #               for col_format, col in zip(table_format, table)]

    print(col_widths)
    return row_delim.join(
            col_delim.join(format.format(cell, width) for format, cell, width in zip(row_format, row, col_widths))
           for row_format, row in zip(table_format, table))


# 不带行名称
def format_matrix2(header,
                   matrix,
                   top_format, left_format, cell_format, row_delim, col_delim):
    table = [header] + [row for row in matrix]
    table_format = [['{:^{}}'] + len(header) * [top_format]] \
                   + len(matrix) * [[left_format] + len(header) * [cell_format]]
    col_widths = [max(
        len(format.format(cell, 0))
        for format, cell in zip(col_format, col))
        for col_format, col in zip(zip(*table_format), zip(*table))]
    return row_delim.join(
        col_delim+col_delim.join(
            format.format(cell, width)
            for format, cell, width in zip(row_format, row, col_widths))+col_delim
        for row_format, row in zip(table_format, table))


print(format_matrix(['Man Utd', 'Man City', 'T Hotspur', 'Really Long Column'],
                    [[1, 2, 1, -1], [0, 1, 0, 5], [2, 4, 2, 2], [0, 1, 0, 6]],
                    '{:^{}}', '{:<{}}', '{:>{}.3f}', '\n', ' | '))
print()
print(format_matrix2(['Man Utd', 'Man City', 'T Hotspur', 'Really Long Column'],
                     [[1, 2, 1, -1], [0, 1, 0, 5], [2, 4, 2, 2], [0, 1, 0, 6]],
                     '{:^{}}', '{:>{}.3f}', '{:>{}.3f}', '\n', ' | '))
  1. 开源的库
https://www.codenong.com/9535954/

https://pypi.python.org/pypi/tableate
https://pypi.python.org/pypi/prettytable
https://pypi.python.org/pypi/texttable

  1. 打印list
str1 = beamer_free_dict['Mem:']  # 1652260 545444 400548 176076 706268 624268
ls = [str(int(c) // 1024) for c in str1.split()] # ['1613', '532', '391', '171', '689', '609']
mem_str2 = "M   ".join(ls)  # 1613M   532M   391M   171M   689M   609
mem_str1 = "{:<}M{:>5}M{:>5}M{:>5}M{:>5}M".format(*ls) # 613M  532M  391M  171M  689M
posted @ 2021-07-14 11:17  该显示昵称已被使用了  阅读(137)  评论(0编辑  收藏  举报