python print【format】【打印table】

时间:2021-07-14
本文章向大家介绍python print【format】【打印table】,主要包括python print【format】【打印table】使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
# 例子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))


原文地址:https://www.cnblogs.com/amize/p/15009982.html