python爱好者.
发表于 2022-1-30 14:57:00
总之你自己试试,负号是偏移了!
python爱好者.
发表于 2022-1-30 14:57:33
我这里不知道为什么?打不出这种效果!
python爱好者.
发表于 2022-1-30 14:59:10
你将 list1 设置为:
list1 = [[-2,-1213214234523543523625362,0],,]
负号偏移的效果更严重!!!
ckblt
发表于 2022-1-30 15:07:45
负号偏移了吗,什么意思
-2 -1213214234523543523625362 0
1342 2 3
234 5 6
ckblt
发表于 2022-1-30 15:08:41
python爱好者. 发表于 2022-1-30 14:59
你将 list1 设置为:
负号偏移的效果更严重!!!
可能是你命令行太小了吧
python爱好者.
发表于 2022-1-30 15:11:43
我重试了一下,好像确实没问题,但是......
python爱好者.
发表于 2022-1-30 15:12:25
还是有个 BUG:
你将 list1 设为:
list1 = [[-2,-121321423243434,0],,]
就会严重便宜了!
ckblt
发表于 2022-1-30 15:13:35
-2 -121321423243434 0
1342 2789879798798 3
234 59789798 6
ckblt
发表于 2022-1-30 15:14:10
python爱好者. 发表于 2022-1-30 15:12
还是有个 BUG:
你将 list1 设为:
有没有截图
python爱好者.
发表于 2022-1-30 15:16:39
ckblt
发表于 2022-1-30 15:18:53
是不是空格偏移?
可能是你空格数多,我空格数少
python爱好者.
发表于 2022-1-30 15:20:05
哦,我数了数,对的!
python爱好者.
发表于 2022-1-30 15:25:04
我又更新了一下我的,现在可以输出方形的了:
list1 = [[-2,-1,0],,]
space = 5
string = ""
space_with_number = (len(str(max(,key = lambda x:len(str(x)))))) * 2 + space
space = []
for i in range(len(list1)):
space.append( for each in each_row]) - len(list1):len( for each in each_row])]]).split())]])
space =
value =
spacer = (space_with_number - len(list1)) // (len(list1) - 1)
i = 0
for x in zip(value,space):
if i % 3 == 0:
print(spacer * "\n")
print(x + x * " ",end = "")
i += 1
python爱好者.
发表于 2022-1-30 15:25:35
其实就是加了个 spacer 变量而已。。。
python爱好者.
发表于 2022-1-30 15:29:29
你也试试?{:10_254:}
ckblt
发表于 2022-1-30 15:29:42
python爱好者. 发表于 2022-1-30 15:25
我又更新了一下我的,现在可以输出方形的了:
可以,不过打印之前会空几行
python爱好者.
发表于 2022-1-30 15:30:34
让我想亿会儿
python爱好者.
发表于 2022-1-30 15:31:24
现在好了:
list1 = [[-221312,-1,0],,]
space = 15
string = ""
space_with_number = (len(str(max(,key = lambda x:len(str(x)))))) * 2 + space
space = []
for i in range(len(list1)):
space.append( for each in each_row]) - len(list1):len( for each in each_row])]]).split())]])
space =
value =
spacer = (space_with_number - len(list1)) // (len(list1) - 1)
i = 1
for x in zip(value,space):
if i % 4 == 0:
print(spacer * "\n")
print(x + x * " ",end = "")
i += 1
python爱好者.
发表于 2022-1-30 15:31:54
改了一下 i 变量,从 0 改成了 1.
ckblt
发表于 2022-1-30 15:32:19
python爱好者. 发表于 2022-1-30 15:31
现在好了:
from typing import Union
def no_list_list(
x: Union, no_tuples: bool = False, no_sets: bool = False
) -> list:
"""
禁止列表套娃函数(使用递归)
返回值: 列表
### 参数:
x: 套娃的 列表 | 元组 | 集合
no_tuples: 禁止元组套娃
no_sets: 禁止集合套娃
### 例子:
```
no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
no_list_list( [ [ [ 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), len(self.table)
self.__check()
"""
检查 self.table 是否合法
"""
def __check(self):
temp = set()
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
# 正式渲染
for i_i in range(len(self.table)):
i = self.table
for j in i:
rendered += str(j) + " " * (col_spaces - len(str(j)))
if i_i != len(self.table) - 1:
rendered += "\n" * (col_spaces // 2)
return rendered
print(Table([, , ]).render())