排除二维嵌套列表中最后一列后的空格

Exclude whitespace after last column in two-dimensional nested list

几乎解决了,但无法弄清楚如何排除第三列数字后的白色 space。代码如下:

mult_table = [
    [1, 2, 3],
    [2, 4, 6],
    [3, 6, 9]
]

for row, nums in enumerate(mult_table):
    for column, num in enumerate(nums):
          print(num, end = ' ')
          if column == len(mult_table)-1:
              print()
          else:
                print('| ', end = '')

Your output (is including white space after column 3)

1 | 2 | 3 
2 | 4 | 6 
3 | 6 | 9 

Expected output (should not include whitespace after column 3)

1 | 2 | 3
2 | 4 | 6
3 | 6 | 9

使用 str.join() 构建该字符串,例如:

代码:

' | '.join(str(x) for x in row)

测试代码:

mult_table = [
    [1, 2, 3],
    [2, 4, 6],
    [3, 6, 9]
]

for row in mult_table:
    print(' | '.join(str(x) for x in row))

结果:

1 | 2 | 3
2 | 4 | 6
3 | 6 | 9