我正在尝试以该面板为中心,这是我尝试使用%* s的代码,但没有成功。
任何的想法?

board = [["1","2","2"],["8*1","2@3","5*6"],["9","5","8"],["2","2","2"],
     ["5*6","6*8","0@2"],["1","2","8"],["1","9","8"],["2*7","7*5","4@2"],["1","3","3"]]

counter = 0

print("--------------+---------------+-------------------")

for row in board:
    counter += 1
    print("|"      "%s"     "|") % ("       |      ".join(row).center(47))
    if counter == 3 or counter == 6 or counter == 9:
        print("---------------+---------------------+--------------")


每个盒子的输出应该像这样

        +-----------+
        | 1 | 1 | 9 |

        |2@3|1*6|7*2|

        | 4 | 1 | 2 |
        +-----------+

最佳答案

我修改了您的代码,使其产生的内容接近您的要求:

board = [["1","2","2"],["8*1","2@3","5*6"],["9","5","8"],
         ["2","2","2"],["5*6","6*8","0@2"],["1","2","8"],
         ["1","9","8"],["2*7","7*5","4@2"],["1","3","3"]]

counter = 0

print("+-----------+")

for row in board:
    counter += 1
    print("|%s|" % "|".join(c.center(3) for c in row))
    if counter == 3 or counter == 6 or counter == 9:
        print("+-----------+")


产生:

+-----------+
| 1 | 2 | 2 |
|8*1|2@3|5*6|
| 9 | 5 | 8 |
+-----------+
| 2 | 2 | 2 |
|5*6|6*8|0@2|
| 1 | 2 | 8 |
+-----------+
| 1 | 9 | 8 |
|2*7|7*5|4@2|
| 1 | 3 | 3 |
+-----------+

关于python - 居中矩阵,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11497485/

10-09 18:40