在没有numpy的情况下,无法在python中打印二维矩阵中的第一行或第二列名称。只能打印矩阵中的浮点数。

问题描述 投票:0回答:1

我想用python打印一个二维矩阵。然而,我只能打印浮动的测试分数,而不能打印与行和列标题对应的字符串。

我想打印的是

studentName ex1 ex2 ex3 mike 78.0 89.0 89.0 Sarah 98.0 78.0 65.0 David 84.0 83.0 98.0

但是只得到了浮点数,却不知道如何添加行和列标题。

这是我到目前为止的成果。

studentName = int(input("enter the number of student: "))
studentExam = int(input("how many exam scores: "))
names_students = []
# Initialize matrix
matrix = []
# For user input
for i in range(studentName):  # A for loop for row entries
    exam_student = []
    names_students.append(input("enter name of students" + str(i+1) + ": "))
    for j in range(studentExam):  # A for loop for column entries
        exam_student.append(float(input("enter exam " + str(j+1) + ": ")))
    matrix.append(exam_student)

#for printing
for i in range(studentName):
    for j in range(studentExam):
        print(matrix [i][j], end=" ")
    print()
python string matrix 2d
1个回答
0
投票

需要在代码中做一点修改,虽然代码的复杂度不高,但效率很高。

studentName = int(input("enter the number of student: "))
studentExam = int(input("how many exam scores: "))
columns = ['name','s1','s2','s3']
# Initialize matrix
matrix = [columns]
# For user input
for i in range(studentName):  # A for loop for row entries
    exam_student = []
    exam_student.append(input("enter name of students" + str(i+1) + ": "))
    for j in range(studentExam):  # A for loop for column entries
        exam_student.append(float(input("enter exam " + str(j+1) + ": ")))
    matrix.append(exam_student)

#for printing
for i in range(len(matrix)):
    for j in range(len(matrix)):
        print(matrix [i][j], end=" ")
    print()
© www.soinside.com 2019 - 2024. All rights reserved.