list1 = [[1,12,3],[4,5,6],[7,8,9]]
alpha = 0 #是否带图表#设置成功!
square = 1 #是否是方形#设置成功!
ch = 0 #设置间距#可以是负数!因为它默认有预留空间!
space_with_number = (len(str(max([each for each_row in list1 for each in each_row],key = lambda x:len(str(x)))))) * 2 + ch
space = [(space_with_number - int(each)) for each in [len(str(each)) for each in [each for each_row in list1 for each in each_row]]]
value = [str(each) for each_row in list1 for each in each_row] #列表中的每个值
spacer = (((len(list1[0][:-1]) * space_with_number) + (len(str(list1[0][-1])))) - (len(list1))) // (len(list1) - 1) // 2 #计算每列每行之间的空隙
length = len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + ((len(list1[0]) * len("|") + 1) if alpha == 1 else 0)
top_or_bottle_words = (length + 2) * "-"
mark_index = [i for i in range(1,length + 1,space_with_number + 1)]
print(top_or_bottle_words) if alpha == 1 else "" #打印开头
for i in range(len(value)): #打印中间部分
print() if i % len(list1[0]) == 0 and i > 1 else "" #如果换行的话
if i % len(list1[0]) == 0 and i > 1 and square == 1: #如果换行要打印的格式是正方形时
for each_spacer in range(spacer):# -x
for each_index in range(length + 2):
print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
print()
print((length + 2) * "-") if alpha == 1 else "" #如果换行要打印的格式是有图表的格式时
if i % len(list1[0]) == 0 and square != 1 and alpha == 1: #如果换行要打印的格式不是方形时
print((length + 2) * "-") if i > 1 else "" #如果换行要打印的格式是有图表的格式时
if i % len(list1[0]) == 0 and alpha == 1: #如果换行要打印的格式是图表时
print(" |",end = "")
print(value[i] + (space[i] * " "),end = ("|" if alpha == 1 else "")) #打印每行的值和空格
print() if i == (len(value) - 1) else "" #如果是最后一次迭代时
if alpha == 1 and square == 1: #打印结尾剩余部分
for each_spacer in range(spacer):
for each_index in range(length + 2):
print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
print()
print(top_or_bottle_words) if alpha == 1 else "" #打印结尾
#尾部空格不对齐的原因是在设置打出时不带图表时,尾部便会对不齐。
#这是因为在选择不带图表打印的时候,将 "-" 和 "|" 和 " |" 替换成了与原来这些字符占格数不同的数量的空格!!!
#不过这问题不大!
from typing import Union
def no_list_list(
x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
) -> list:
"""
禁止列表套娃函数(使用递归)
返回值: 列表
### 参数:
x: 套娃的 列表 | 元组 | 集合
no_tuples: 禁止元组套娃
no_sets: 禁止集合套娃
### 例子:
```
no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
[1, 2, 3, 4, 5]
no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
[1, 2, 3, 4, 5, 6, 7]
```
"""
new_list = []
for i in x:
if (
(isinstance(i, list))
or (no_tuples and isinstance(i, tuple))
or (no_sets and isinstance(i, set))
):
new_list.extend(no_list_list(i, no_tuples, no_sets))
else:
new_list.append(i)
return new_list
class Table:
def __init__(self, table: list):
"""
初始化 Table
"""
self.table = table
self.length = len(self.table[0]), len(self.table)
self.__check()
def __check(self):
"""
检查 self.table 是否合法
"""
temp = set([len(x) for x in self.table])
if len(temp) != 1:
raise Exception()
def render(self, isChart=False, isSquare=False, spacing=4):#####这里 isChart 和 isSquare 改变后输出的结果却一直不变!
"""
渲染 self.table
参数:
isChart: bool -- 外加线条
isSquare: bool -- 正方形模式
spacing: int -- 间距, 需要大于 0
"""
col_spaces = spacing
l = no_list_list(self.table)
rendered = ""
# 每列之间的空,存储到 col_spaces
for i in l:
if len(str(i)) > col_spaces - spacing:
col_spaces = len(str(i)) + spacing
# 正式渲染
if isChart:
rendered += "-" * ((col_spaces + 1) * self.length[0] + 1) + "\n"
for i_i in range(self.length[1]):
i = self.table[i_i]
if isChart:
rendered += "|"
for j in i:
rendered += str(j) + " " * (col_spaces - len(str(j)))
if isChart:
rendered += "|"
if isSquare:
if isChart:
rendered += "\n" + (
("|" + " " * col_spaces) * (self.length[0] + 1) + "\n"
) * (col_spaces // 3)
elif i_i != self.length[1] - 1:
rendered += "\n" * (col_spaces // 3)
else:
rendered += "\n"
if isChart:
rendered += "-" * ((col_spaces + 1) * self.length[0] + 1) + "\n"
return rendered
print(Table(list1).render(True, True, 4))
上面是我的,下面是你的,结果明显不一样,你把参数再改一下,它的结果还是不会变:
[code]1 12 3
4 5 6
7 8 9
----------------------
|1 |12 |3 |
| | | |
| | | |
----------------------
|4 |5 |6 |
| | | |
| | | |
----------------------
|7 |8 |9 |
| | | |
| | | |
----------------------