0.计算嵌套列表元素的最大宽度


import copy

data = [
            ['姓名','性别','年龄','手机号','固定电话','邮箱'],
            ['厘清','女','31','18745214693','0104784125','5412546qq.com'],
            ['张三','男','26','18245554693','010-4784125','无'],
            ['王武','男','19','13245266693','无','785992546qq.com'],
            ['熊大','男','16','无','010-4784125','115412546qq.com'],
            ['熊二','男','22','18745214693','010-4784125','3654126qq.com']
    ]

# 获取每列所占用的最大列宽
def get_max_col(max_list):
    line_list = []
    # i表示行,j代表列
    for j in range(len(max_list[0])):
        line_num = []
        for i in range(len(max_list)):
            line_num.append(max_list[i][j])  # 将每列的宽度存入line_num
        line_list.append(max(line_num))  # 将每列最大宽度存入line_list
    return line_list

col_list = []  # 记录每行宽度

col_num = [0 for x in range(0, len(data[0]))]   # 记录每行每个数据宽度,[0, 0, 0, 0, 0, 0]  每行6个元素,默认宽度为0

# 计算
for i in range(0, len(data)):
    print(data[i])
    for j in range(0, len(data[i])):
        col_num[j] = len(data[i][j].encode('gb18030'))  # 计算每列值的大小
        print(col_num)
    col_list.append(copy.copy(col_num))  # 记录一行每列写入的长度,使用copy
    print(col_list)

print(col_list)

# 获取每列最大宽度
col_max_num = get_max_col(col_list)

print(col_max_num)