简体   繁体   English

python中单个print()函数的结果是什么

[英]What's the result of a single print() function in python

I got a python sublist and a function(replace 0 with .) to print this sublist(Yeah, it's like sudoku).我有一个 python 子列表和一个函数(用 . 替换 0)来打印这个子列表(是的,就像数独)。 The code is as below.代码如下。

 grid = [[5,3,0,0,7,0,0,0,0], [6,0,0,1,9,5,0,0,0], [0,9,8,0,0,0,0,6,0], [8,0,0,0,6,0,0,0,3], [4,0,0,8,0,3,0,0,1], [7,0,0,0,2,0,0,0,6], [0,6,0,0,0,0,2,8,0], [0,0,0,4,1,9,0,0,5], [0,0,0,0,8,0,0,7,9]] def print_grid(): for row in grid: for column in row: if column == 0: print("." , end=" ") else: print(column , end=" ")

After I run print_grid(), I got a result like below.运行print_grid() 后,得到如下结果。 I wonder how to print it line by line.我想知道如何逐行打印它。 Thanks in advance for any kind of help.在此先感谢您的任何帮助。

 3 . . 7 . . . . 6 . . 1 9 5 . . . . 9 8 . . . . 6 . 8 . . . 6 . . . 3 4 . . 8 . 3 . . 1 7 . . . 2 . . . 6 . 6 . . . . 2 8 . . . . 4 1 9 . . 5 . . . . 8 . . 7 9

Add a print() at the end of second for loop:在第二个 for 循环的末尾添加一个print()

grid = [[5,3,0,0,7,0,0,0,0], 
        [6,0,0,1,9,5,0,0,0], 
        [0,9,8,0,0,0,0,6,0], 
        [8,0,0,0,6,0,0,0,3], 
        [4,0,0,8,0,3,0,0,1], 
        [7,0,0,0,2,0,0,0,6], 
        [0,6,0,0,0,0,2,8,0], 
        [0,0,0,4,1,9,0,0,5], 
        [0,0,0,0,8,0,0,7,9]]
        
def print_grid():
    for row in grid:
        for column in row:
            if column == 0:
                print("." , end=" ")
            else:
                print(column , end=" ")
        print()

print_grid()

This achieves the result you are looking for in a simpler way, printing each row of the grid "line by line".这以更简单的方式实现了您正在寻找的结果,“逐行”打印网格的每一行。 For each row we use a list comprehension to generate a new list of the row values where 0 is replaced with "."对于每一行,我们使用列表理解来生成行值的新列表,其中 0 替换为“。” Then using the splat operator (*) we pass this row as arguments which the print statement will print with spaces in between.然后使用 splat 运算符 (*) 我们将此行作为参数传递,print 语句将在其间打印空格。

grid = [[5,3,0,0,7,0,0,0,0], 
    [6,0,0,1,9,5,0,0,0], 
    [0,9,8,0,0,0,0,6,0], 
    [8,0,0,0,6,0,0,0,3], 
    [4,0,0,8,0,3,0,0,1], 
    [7,0,0,0,2,0,0,0,6], 
    [0,6,0,0,0,0,2,8,0], 
    [0,0,0,4,1,9,0,0,5], 
    [0,0,0,0,8,0,0,7,9]]

for row in grid:
    print(*["." if val == 0 else val for val in row])  

The module Numpy is perfect for this. Numpy模块非常适合这一点。 But also it is required that your print function becomes a board modifying function.但也需要您的打印功能成为板卡修改功能。

grid = [[5,3,0,0,7,0,0,0,0], 
    [6,0,0,1,9,5,0,0,0], 
    [0,9,8,0,0,0,0,6,0], 
    [8,0,0,0,6,0,0,0,3], 
    [4,0,0,8,0,3,0,0,1], 
    [7,0,0,0,2,0,0,0,6], 
    [0,6,0,0,0,0,2,8,0], 
    [0,0,0,4,1,9,0,0,5], 
    [0,0,0,0,8,0,0,7,9]]
    
def change_grid():
    for x in range(9):
        for y in range(9):
            if grid[x][y] == 0:
                grid[x][y] = "." 
change_grid()
print(np.matrix(grid))

All you need to do is to change a line everytime you loop through one row.您需要做的就是每次循环遍历一行时更改一行。

def print_grid():
    for row in grid:
        print("")
        for column in row:
            if column == 0:
                print("." , end=" ")
            else:
                print(column , end=" ")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM