(Python初学者)函数中的矩阵打印

时间:2022-02-26 16:00:41

I've written a simple function as well as input for it, but I am not sure what to put into the "pass" for my desired output. Here is my code:

我已经编写了一个简单的函数以及它的输入,但是我不确定将什么输入“pass”以获得我想要的输出。这是我的代码:

def print_matrix(matrix_in, rows, columns, matrix):
    pass

    def print_header(columns):
        line = "+"
        for i in range(columns):
            line += "---+"
        print(line)

matrix={(2, 2): 5, (1, 2): 4, (0, 1): 2, (0, 0): 1, (1, 1): 3, (2, 3): 6}
rows=3
columns=4
matrix="Matrix 1"

print_matrix(matrix, rows, columns, matrix)

For this desired output:

对于这个期望的输出:

Matrix 1

+---+---+---+---+
|  1|  2|  0|  0|
+---+---+---+---+
|  0|  3|  4|  0|
+---+---+---+---+
|  0|  0|  5|  6|
+---+---+---+---+

Any help would be appreciated thank you.

任何帮助将不胜感激,谢谢。

2 个解决方案

#1


0  

Try this one:

试试这个:

def print_matrix(matrixIn, rows, columns, matrixName):
    print(matrixName + "\n")

    for i in range(rows):
        print("+---" * (rows+1) + "+")
        for j in range(columns):
            key = (i,j)
            value = matrixIn.get(key)
            if value is None:
                print("| ", 0, end="")
            else:
                print("| ", value, end="")
        print("|")
    print("+---" * (rows+1) + "+")

matrix={(2, 2): 5, (1, 2): 4, (0, 1): 2, (0, 0): 1, (1, 1): 3, (2, 3): 6}
rows=3
columns=4
matrixString="Matrix 1"

print_matrix(matrix, rows, columns, matrixString)

The result is:

结果是:

Matrix 1

+---+---+---+---+
|  1|  2|  0|  0|
+---+---+---+---+
|  0|  3|  4|  0|
+---+---+---+---+
|  0|  0|  5|  6|
+---+---+---+---+

#2


0  

Not sure about dictionaries, with a list I would do

不确定字典,我会做一个列表

def print_matrix(matrix):
   for nr in range(len(matrix[:])):
      print '+----------------+'
      print '| '+' | '.join(str(i) for i in matrix[nr][:])+' |'
   print '+----------------+'
return

matrix = [[1,2,0,0],[0,3,4,0],[0,0,5,6]]
print_matrix(matrix) 

#1


0  

Try this one:

试试这个:

def print_matrix(matrixIn, rows, columns, matrixName):
    print(matrixName + "\n")

    for i in range(rows):
        print("+---" * (rows+1) + "+")
        for j in range(columns):
            key = (i,j)
            value = matrixIn.get(key)
            if value is None:
                print("| ", 0, end="")
            else:
                print("| ", value, end="")
        print("|")
    print("+---" * (rows+1) + "+")

matrix={(2, 2): 5, (1, 2): 4, (0, 1): 2, (0, 0): 1, (1, 1): 3, (2, 3): 6}
rows=3
columns=4
matrixString="Matrix 1"

print_matrix(matrix, rows, columns, matrixString)

The result is:

结果是:

Matrix 1

+---+---+---+---+
|  1|  2|  0|  0|
+---+---+---+---+
|  0|  3|  4|  0|
+---+---+---+---+
|  0|  0|  5|  6|
+---+---+---+---+

#2


0  

Not sure about dictionaries, with a list I would do

不确定字典,我会做一个列表

def print_matrix(matrix):
   for nr in range(len(matrix[:])):
      print '+----------------+'
      print '| '+' | '.join(str(i) for i in matrix[nr][:])+' |'
   print '+----------------+'
return

matrix = [[1,2,0,0],[0,3,4,0],[0,0,5,6]]
print_matrix(matrix)