我已经写了一个简单的函数以及它的输入,但我不知道该放入"传递"为了我想要的输出。这是我的代码:
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)
对于此期望的输出:
Matrix 1
+---+---+---+---+
| 1| 2| 0| 0|
+---+---+---+---+
| 0| 3| 4| 0|
+---+---+---+---+
| 0| 0| 5| 6|
+---+---+---+---+
任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
不确定字典,我会做一个列表
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)
试试这个:
deselect instant run
结果是:
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)