鱼C论坛

 找回密码
 立即注册
查看: 1989|回复: 72

[作品展示] 【Python】命令行表格渲染器

[复制链接]
发表于 2022-1-25 22:23:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ckblt 于 2022-1-25 22:23 编辑

最近无聊编了一个命令行表格渲染器
我给大家分享一下(有 BUG 请跟我说):

  1. class Table:
  2.     """
  3.     初始化 Table
  4.     """

  5.     def __init__(self, table: list):
  6.         self.table = table
  7.         self.length = len(self.table[0]), len(self.table)
  8.         self.__check()

  9.     """
  10.     检查 self.table 是否合法
  11.     """

  12.     def __check(self):
  13.         temp = set([len(x) for x in self.table])
  14.         if len(temp) != 1:
  15.             raise Exception()

  16.     """
  17.     渲染 self.table
  18.     """

  19.     def render(self):
  20.         col_spaces = []
  21.         table2 = []
  22.         rendered = ""

  23.         # 把 table 的 x轴和 y轴互换,存储到 table2
  24.         for i in range(self.length[0]):
  25.             temp = []
  26.             for j in range(self.length[1]):
  27.                 temp.append(str(self.table[j][i]))
  28.             table2.append(temp)

  29.         # 每列之间的空,存储到 col_spaces
  30.         for i in table2:
  31.             j = [len(x) for x in i]
  32.             col_spaces.append(max(*j) + 4)

  33.         # 正式渲染
  34.         for i in self.table:
  35.             for j_i in range(len(i)):
  36.                 j = str(i[j_i])
  37.                 rendered += j + " " * (col_spaces[j_i] - len(j))
  38.             rendered += "\n"

  39.         return rendered
复制代码


使用方法(不会的话可以再问我):
  1. t = Table([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
  2. # [ [1,  2,  3],
  3. #   [4,  5,  6],
  4. #   [7,  8,  9],
  5. #   [10, 11, 12] ]

  6. print(t.render())
  7. # 输出:
  8. # 1     2     3
  9. # 4     5     6
  10. # 7     8     9
  11. # 10    11    12
复制代码

评分

参与人数 1鱼币 +5 收起 理由
python爱好者. + 5 不错,比我的好太多了!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-1-26 08:42:50 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-1-30 13:52:14 | 显示全部楼层
等会,似乎和我的不太一样,你的似乎不能做到真正意义上的对齐:
这里将多为列表 list1 设置为:
  1. list1 = [[-2,-1111111,0],[1,2,3],[4,5,6]]
复制代码

然后运行我们两个的代码,上面是我的,下面是你的:
  1. list1 = [[-2,-1111111,0],[1,2,3],[4,5,6]]
  2. space = 0
  3. string = ""
  4. 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
  5. space = []
  6. for i in range(len(list1)):

  7.     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())]])
  8. space = [each for each_line in space for each in each_line]
  9. value = [str(each) for each_line in list1 for each in each_line]
  10. i = 0
  11. for x in zip(value,space):
  12.     if i % 3 == 0:
  13.         print()
  14.     print(x[0] + x[1] * " ",end = "")
  15.     i += 1
  16. print()






  17. class Table:
  18.     """
  19.     初始化 Table
  20.     """

  21.     def __init__(self, table: list):
  22.         self.table = table
  23.         self.length = len(self.table[0]), len(self.table)
  24.         self.__check()

  25.     """
  26.     检查 self.table 是否合法
  27.     """

  28.     def __check(self):
  29.         temp = set([len(x) for x in self.table])
  30.         if len(temp) != 1:
  31.             raise Exception()

  32.     """
  33.     渲染 self.table
  34.     """

  35.     def render(self):
  36.         col_spaces = []
  37.         table2 = []
  38.         rendered = ""

  39.         # 把 table 的 x轴和 y轴互换,存储到 table2
  40.         for i in range(self.length[0]):
  41.             temp = []
  42.             for j in range(self.length[1]):
  43.                 temp.append(str(self.table[j][i]))
  44.             table2.append(temp)

  45.         # 每列之间的空,存储到 col_spaces
  46.         for i in table2:
  47.             j = [len(x) for x in i]
  48.             col_spaces.append(max(*j) + 4)

  49.         # 正式渲染
  50.         for i in self.table:
  51.             for j_i in range(len(i)):
  52.                 j = str(i[j_i])
  53.                 rendered += j + " " * (col_spaces[j_i] - len(j))
  54.             rendered += "\n"

  55.         return rendered
  56. t = Table(list1)

  57. print(t.render())
复制代码

输出结果是:
  1. -2              -1111111        0               
  2. 1               2               3               
  3. 4               5               6               
  4. -2    -1111111    0   
  5. 1     2           3   
  6. 4     5           6   
复制代码

上面是我的,下面是你的,我复制时可能有些差错,您可以自己试一下!
所以你的其实没有去设置对齐的代码!是不是!?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-30 13:56:10 | 显示全部楼层
不是呀,
我的输出是:
  1. -2    -1111111    0   
  2. 1     2           3   
  3. 4     5           6   
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-30 13:56:52 | 显示全部楼层
python爱好者. 发表于 2022-1-30 13:52
等会,似乎和我的不太一样,你的似乎不能做到真正意义上的对齐:
这里将多为列表 list1 设置为:

不是呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-30 13:58:03 | 显示全部楼层
ckblt 发表于 2022-1-30 13:56
不是呀,
我的输出是:

我前面就是说你的输出的是:
  1. -2    -1111111    0   
  2. 1     2           3   
  3. 4     5           6   
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-30 13:58:27 | 显示全部楼层
python爱好者. 发表于 2022-1-30 13:52
等会,似乎和我的不太一样,你的似乎不能做到真正意义上的对齐:
这里将多为列表 list1 设置为:

哦,不好意思,理解错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-30 14:00:19 | 显示全部楼层
我的表格是单独两个列之间的的对齐,你的是全部对齐,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-1-30 14:00:21 | 显示全部楼层

但是您看这里第 1 列与第 2 列相差 4 个空格,
而第 2 列与第 3 列相差 11 个空格,所以没有对齐!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-30 14:01:52 | 显示全部楼层
ckblt 发表于 2022-1-30 14:00
我的表格是单独两个列之间的的对齐,你的是全部对齐,

没事,您列之间对齐都能做的这么精简,相信这对你来说也是十分轻松的事。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-30 14:09:17 | 显示全部楼层
python爱好者. 发表于 2022-1-30 14:01
没事,您列之间对齐都能做的这么精简,相信这对你来说也是十分轻松的事。

按照你的建议改了改代码:
(里面用到了禁止套娃函数 no_list_list)
  1. from typing import Union


  2. def no_list_list(
  3.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  4. ) -> list:
  5.     """
  6.     禁止列表套娃函数(使用递归)
  7.     返回值: 列表

  8.     ### 参数:

  9.     x: 套娃的 列表 | 元组 | 集合
  10.     no_tuples: 禁止元组套娃
  11.     no_sets: 禁止集合套娃

  12.     ### 例子:

  13.     ```
  14.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  15.     [1, 2, 3, 4, 5]

  16.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  17.     [1, 2, 3, 4, 5, 6, 7]
  18.     ```
  19.     """

  20.     new_list = []

  21.     for i in x:
  22.         if (
  23.             (isinstance(i, list))
  24.             or (no_tuples and isinstance(i, tuple))
  25.             or (no_sets and isinstance(i, set))
  26.         ):
  27.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  28.         else:
  29.             new_list.append(i)

  30.     return new_list


  31. class Table:
  32.     """
  33.     初始化 Table
  34.     """

  35.     def __init__(self, table: list):
  36.         self.table = table
  37.         self.length = len(self.table[0]), len(self.table)
  38.         self.__check()

  39.     """
  40.     检查 self.table 是否合法
  41.     """

  42.     def __check(self):
  43.         temp = set([len(x) for x in self.table])
  44.         if len(temp) != 1:
  45.             raise Exception()

  46.     """
  47.     渲染 self.table
  48.     """

  49.     def render(self):
  50.         col_spaces = 0
  51.         l = no_list_list(self.table)
  52.         rendered = ""


  53.         # 每列之间的空,存储到 col_spaces
  54.         for i in l:
  55.             if i > col_spaces:
  56.                 col_spaces = len(str(i)) + 4

  57.         # 正式渲染
  58.         for i in self.table:
  59.             for j in i:
  60.                 rendered += str(j) + " " * (col_spaces - len(str(j)))
  61.             rendered += "\n"

  62.         return rendered


  63. print(Table([[1, 2, 3], [4, 5, 6], [123456, 8, 9]]).render())
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-30 14:15:02 | 显示全部楼层
还是有 Bug :
  1. list1 = [[-2123,-1121221,0],[1,3442,3],[4,5,6]]
  2. space = 0
  3. string = ""
  4. 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
  5. space = []
  6. for i in range(len(list1)):

  7.     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())]])
  8. space = [each for each_line in space for each in each_line]
  9. value = [str(each) for each_line in list1 for each in each_line]
  10. i = 0
  11. for x in zip(value,space):
  12.     if i % 3 == 0:
  13.         print()
  14.     print(x[0] + x[1] * " ",end = "")
  15.     i += 1
  16. print()






  17. from typing import Union


  18. def no_list_list(
  19.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  20. ) -> list:
  21.     """
  22.     禁止列表套娃函数(使用递归)
  23.     返回值: 列表

  24.     ### 参数:

  25.     x: 套娃的 列表 | 元组 | 集合
  26.     no_tuples: 禁止元组套娃
  27.     no_sets: 禁止集合套娃

  28.     ### 例子:

  29.     ```
  30.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  31.     [1, 2, 3, 4, 5]

  32.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  33.     [1, 2, 3, 4, 5, 6, 7]
  34.     ```
  35.     """

  36.     new_list = []

  37.     for i in x:
  38.         if (
  39.             (isinstance(i, list))
  40.             or (no_tuples and isinstance(i, tuple))
  41.             or (no_sets and isinstance(i, set))
  42.         ):
  43.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  44.         else:
  45.             new_list.append(i)

  46.     return new_list


  47. class Table:
  48.     """
  49.     初始化 Table
  50.     """

  51.     def __init__(self, table: list):
  52.         self.table = table
  53.         self.length = len(self.table[0]), len(self.table)
  54.         self.__check()

  55.     """
  56.     检查 self.table 是否合法
  57.     """

  58.     def __check(self):
  59.         temp = set([len(x) for x in self.table])
  60.         if len(temp) != 1:
  61.             raise Exception()

  62.     """
  63.     渲染 self.table
  64.     """

  65.     def render(self):
  66.         col_spaces = 0
  67.         l = no_list_list(self.table)
  68.         rendered = ""


  69.         # 每列之间的空,存储到 col_spaces
  70.         for i in l:
  71.             if i > col_spaces:
  72.                 col_spaces = len(str(i)) + 4

  73.         # 正式渲染
  74.         for i in self.table:
  75.             for j in i:
  76.                 rendered += str(j) + " " * (col_spaces - len(str(j)))
  77.             rendered += "\n"

  78.         return rendered


  79. print(Table(list1).render())
复制代码

上面的是我的,下面的是你的,结果是:
  1. -2123           -1121221        0               
  2. 1               3442            3               
  3. 4               5               6               
  4. -2123   -11212210      
  5. 1       3442    3      
  6. 4       5       6      
复制代码

你的最后两列怎么粘在一起了?!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-30 14:24:07 | 显示全部楼层
python爱好者. 发表于 2022-1-30 14:15
还是有 Bug :

上面的是我的,下面的是你的,结果是:
  1. from typing import Union


  2. def no_list_list(
  3.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  4. ) -> list:
  5.     """
  6.     禁止列表套娃函数(使用递归)
  7.     返回值: 列表

  8.     ### 参数:

  9.     x: 套娃的 列表 | 元组 | 集合
  10.     no_tuples: 禁止元组套娃
  11.     no_sets: 禁止集合套娃

  12.     ### 例子:

  13.     ```
  14.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  15.     [1, 2, 3, 4, 5]

  16.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  17.     [1, 2, 3, 4, 5, 6, 7]
  18.     ```
  19.     """

  20.     new_list = []

  21.     for i in x:
  22.         if (
  23.             (isinstance(i, list))
  24.             or (no_tuples and isinstance(i, tuple))
  25.             or (no_sets and isinstance(i, set))
  26.         ):
  27.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  28.         else:
  29.             new_list.append(i)

  30.     return new_list


  31. class Table:
  32.     """
  33.     初始化 Table
  34.     """

  35.     def __init__(self, table: list):
  36.         self.table = table
  37.         self.length = len(self.table[0]), len(self.table)
  38.         self.__check()

  39.     """
  40.     检查 self.table 是否合法
  41.     """

  42.     def __check(self):
  43.         temp = set([len(x) for x in self.table])
  44.         if len(temp) != 1:
  45.             raise Exception()

  46.     """
  47.     渲染 self.table
  48.     """

  49.     def render(self):
  50.         col_spaces = 4
  51.         l = no_list_list(self.table)
  52.         rendered = ""


  53.         # 每列之间的空,存储到 col_spaces
  54.         for i in l:
  55.             if len(str(i)) > col_spaces - 4:
  56.                 col_spaces = len(str(i)) + 4
  57.                 print(col_spaces)


  58.         # 正式渲染
  59.         for i in self.table:
  60.             for j in i:
  61.                 rendered += str(j) + " " * (col_spaces - len(str(j)))
  62.             rendered += "\n"

  63.         return rendered


  64. print(Table([[-2123,-1121221,0],[1,3442,3],[4,5,6]]).render())
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-30 14:29:49 | 显示全部楼层
还还还是有 BUG:
  1. list1 = [[-2,-1213214234,0],[1342,2,3],[234,5,6]]
  2. space = 5
  3. string = ""
  4. 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
  5. space = []
  6. for i in range(len(list1)):

  7.     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())]])
  8. space = [each for each_line in space for each in each_line]
  9. value = [str(each) for each_line in list1 for each in each_line]
  10. i = 0
  11. for x in zip(value,space):
  12.     if i % 3 == 0:
  13.         print()
  14.     print(x[0] + x[1] * " ",end = "")
  15.     i += 1











  16. from typing import Union


  17. def no_list_list(
  18.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  19. ) -> list:
  20.     """
  21.     禁止列表套娃函数(使用递归)
  22.     返回值: 列表

  23.     ### 参数:

  24.     x: 套娃的 列表 | 元组 | 集合
  25.     no_tuples: 禁止元组套娃
  26.     no_sets: 禁止集合套娃

  27.     ### 例子:

  28.     ```
  29.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  30.     [1, 2, 3, 4, 5]

  31.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  32.     [1, 2, 3, 4, 5, 6, 7]
  33.     ```
  34.     """

  35.     new_list = []

  36.     for i in x:
  37.         if (
  38.             (isinstance(i, list))
  39.             or (no_tuples and isinstance(i, tuple))
  40.             or (no_sets and isinstance(i, set))
  41.         ):
  42.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  43.         else:
  44.             new_list.append(i)

  45.     return new_list


  46. class Table:
  47.     """
  48.     初始化 Table
  49.     """

  50.     def __init__(self, table: list):
  51.         self.table = table
  52.         self.length = len(self.table[0]), len(self.table)
  53.         self.__check()

  54.     """
  55.     检查 self.table 是否合法
  56.     """

  57.     def __check(self):
  58.         temp = set([len(x) for x in self.table])
  59.         if len(temp) != 1:
  60.             raise Exception()

  61.     """
  62.     渲染 self.table
  63.     """

  64.     def render(self):
  65.         col_spaces = 4
  66.         l = no_list_list(self.table)
  67.         rendered = ""


  68.         # 每列之间的空,存储到 col_spaces
  69.         for i in l:
  70.             if len(str(i)) > col_spaces - 4:
  71.                 col_spaces = len(str(i)) + 4
  72.                 print(col_spaces)


  73.         # 正式渲染
  74.         for i in self.table:
  75.             for j in i:
  76.                 rendered += str(j) + " " * (col_spaces - len(str(j)))
  77.             rendered += "\n"

  78.         return rendered


  79. 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]
上面是我的,下面是你的,结果是:
  1. -2                         -1213214234                0                          
  2. 1342                       2                          3                          
  3. 234                        5                          6                          6
  4. 15
  5. -2             -1213214234    0              
  6. 1342           2              3              
  7. 234            5              6           
复制代码

对齐是对齐了,可这多出来的 15 和 6 又是从何而来呢??!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-30 14:30:30 | 显示全部楼层
中间那一段代码不算昂——刚才发的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-30 14:33:58 | 显示全部楼层

忘记删掉debug了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-30 14:34:37 | 显示全部楼层
  1. from typing import Union


  2. def no_list_list(
  3.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  4. ) -> list:
  5.     """
  6.     禁止列表套娃函数(使用递归)
  7.     返回值: 列表

  8.     ### 参数:

  9.     x: 套娃的 列表 | 元组 | 集合
  10.     no_tuples: 禁止元组套娃
  11.     no_sets: 禁止集合套娃

  12.     ### 例子:

  13.     ```
  14.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  15.     [1, 2, 3, 4, 5]

  16.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  17.     [1, 2, 3, 4, 5, 6, 7]
  18.     ```
  19.     """

  20.     new_list = []

  21.     for i in x:
  22.         if (
  23.             (isinstance(i, list))
  24.             or (no_tuples and isinstance(i, tuple))
  25.             or (no_sets and isinstance(i, set))
  26.         ):
  27.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  28.         else:
  29.             new_list.append(i)

  30.     return new_list


  31. class Table:
  32.     """
  33.     初始化 Table
  34.     """

  35.     def __init__(self, table: list):
  36.         self.table = table
  37.         self.length = len(self.table[0]), len(self.table)
  38.         self.__check()

  39.     """
  40.     检查 self.table 是否合法
  41.     """

  42.     def __check(self):
  43.         temp = set([len(x) for x in self.table])
  44.         if len(temp) != 1:
  45.             raise Exception()

  46.     """
  47.     渲染 self.table
  48.     """

  49.     def render(self):
  50.         col_spaces = 4
  51.         l = no_list_list(self.table)
  52.         rendered = ""


  53.         # 每列之间的空,存储到 col_spaces
  54.         for i in l:
  55.             if len(str(i)) > col_spaces - 4:
  56.                 col_spaces = len(str(i)) + 4


  57.         # 正式渲染
  58.         for i in self.table:
  59.             for j in i:
  60.                 rendered += str(j) + " " * (col_spaces - len(str(j)))
  61.             rendered += "\n"

  62.         return rendered


  63. print(Table([[-2,-1213214234,0],[1342,2,3],[234,5,6]]).render())
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-1-30 14:53:57 | 显示全部楼层
还还还还还是有 BUG:
  1. list1 = [[-2,-1213214234,0],[1342,2,3],[234,5,6]]
  2. space = 5
  3. string = ""
  4. 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
  5. space = []
  6. for i in range(len(list1)):

  7.     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())]])
  8. space = [each for each_line in space for each in each_line]
  9. value = [str(each) for each_line in list1 for each in each_line]
  10. i = 0
  11. for x in zip(value,space):
  12.     if i % 3 == 0:
  13.         print()
  14.     print(x[0] + x[1] * " ",end = "")
  15.     i += 1











  16. from typing import Union


  17. def no_list_list(
  18.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  19. ) -> list:
  20.     """
  21.     禁止列表套娃函数(使用递归)
  22.     返回值: 列表

  23.     ### 参数:

  24.     x: 套娃的 列表 | 元组 | 集合
  25.     no_tuples: 禁止元组套娃
  26.     no_sets: 禁止集合套娃

  27.     ### 例子:

  28.     ```
  29.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  30.     [1, 2, 3, 4, 5]

  31.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  32.     [1, 2, 3, 4, 5, 6, 7]
  33.     ```
  34.     """

  35.     new_list = []

  36.     for i in x:
  37.         if (
  38.             (isinstance(i, list))
  39.             or (no_tuples and isinstance(i, tuple))
  40.             or (no_sets and isinstance(i, set))
  41.         ):
  42.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  43.         else:
  44.             new_list.append(i)

  45.     return new_list


  46. class Table:
  47.     """
  48.     初始化 Table
  49.     """

  50.     def __init__(self, table: list):
  51.         self.table = table
  52.         self.length = len(self.table[0]), len(self.table)
  53.         self.__check()

  54.     """
  55.     检查 self.table 是否合法
  56.     """

  57.     def __check(self):
  58.         temp = set([len(x) for x in self.table])
  59.         if len(temp) != 1:
  60.             raise Exception()

  61.     """
  62.     渲染 self.table
  63.     """

  64.     def render(self):
  65.         col_spaces = 4
  66.         l = no_list_list(self.table)
  67.         rendered = ""


  68.         # 每列之间的空,存储到 col_spaces
  69.         for i in l:
  70.             if len(str(i)) > col_spaces - 4:
  71.                 col_spaces = len(str(i)) + 4


  72.         # 正式渲染
  73.         for i in self.table:
  74.             for j in i:
  75.                 rendered += str(j) + " " * (col_spaces - len(str(j)))
  76.             rendered += "\n"

  77.         return rendered


  78. print(Table(list1).render())

复制代码

上面是我的代码,下面是你的代码,结果是:
  1. -2                         -1213214234                0                          
  2. 1342                       2                          3                          
  3. 234                        5                          6                          -2             -1213214234    0              
  4. 1342           2              3              
  5. 234            5              6         
复制代码

上面是我的,下面是你的,
你这里负号的位置怎么偏了呀!!?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-30 14:54:39 | 显示全部楼层
说错了,结果是:
  1. -2                         -1213214234                0                          
  2. 1342                       2                          3                          
  3. 234                        5                          6                        
  4. -2             -1213214234    0              
  5. 1342           2              3              
  6. 234            5              6         
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-30 14:55:10 | 显示全部楼层
哎,不对,又打错了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 16:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表