|
发表于 2022-1-30 14:29:49
|
显示全部楼层
还还还是有 BUG:list1 = [[-2,-1213214234,0],[1342,2,3],[234,5,6]]
space = 5
string = ""
space_with_number = (len(str(max([each for each_row in list1 for each in each_row],key = lambda x:len(str(x)))))) * 2 + space
space = []
for i in range(len(list1)):
space.append([int(space_with_number - len(each)) for each in [each_row for each_row in (" ".join([str(each) for each in [each for each_row in list1 for each in each_row][len([each for each_row in list1[:i + 1] for each in each_row]) - len(list1[i]):len([each for each_row in list1[:i + 1] for each in each_row])]]).split())]])
space = [each for each_line in space for each in each_line]
value = [str(each) for each_line in list1 for each in each_line]
i = 0
for x in zip(value,space):
if i % 3 == 0:
print()
print(x[0] + x[1] * " ",end = "")
i += 1
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:
"""
初始化 Table
"""
def __init__(self, table: list):
self.table = table
self.length = len(self.table[0]), len(self.table)
self.__check()
"""
检查 self.table 是否合法
"""
def __check(self):
temp = set([len(x) for x in self.table])
if len(temp) != 1:
raise Exception()
"""
渲染 self.table
"""
def render(self):
col_spaces = 4
l = no_list_list(self.table)
rendered = ""
# 每列之间的空,存储到 col_spaces
for i in l:
if len(str(i)) > col_spaces - 4:
col_spaces = len(str(i)) + 4
print(col_spaces)
# 正式渲染
for i in self.table:
for j in i:
rendered += str(j) + " " * (col_spaces - len(str(j)))
rendered += "\n"
return rendered
print(Table(list1).render())
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:
"""
初始化 Table
"""
def __init__(self, table: list):
self.table = table
self.length = len(self.table[0]), len(self.table)
self.__check()
"""
检查 self.table 是否合法
"""
def __check(self):
temp = set([len(x) for x in self.table])
if len(temp) != 1:
raise Exception()
"""
渲染 self.table
"""
def render(self):
col_spaces = 4
l = no_list_list(self.table)
rendered = ""
# 每列之间的空,存储到 col_spaces
for i in l:
if len(str(i)) > col_spaces - 4:
col_spaces = len(str(i)) + 4
print(col_spaces)
# 正式渲染
for i in self.table:
for j in i:
rendered += str(j) + " " * (col_spaces - len(str(j)))
rendered += "\n"
return rendered
print(Table(list1).render())
[/code]
上面是我的,下面是你的,结果是:-2 -1213214234 0
1342 2 3
234 5 6 6
15
-2 -1213214234 0
1342 2 3
234 5 6
对齐是对齐了,可这多出来的 15 和 6 又是从何而来呢??! |
|