繁体   English   中英

如果没有 numpy,则无法在 python 的二维矩阵中打印名称的第一行或第一列。只会打印矩阵中的浮点数

[英]can't print first row or column of names in a 2D matrix in python without numpy. Will only print the float numbers in the matrix

我正在尝试在 python 中打印一个二维矩阵。但是,我只能打印浮动测试分数,而不能打印与行标题和列标题相对应的字符串。

我正在尝试打印这个:

学生姓名 ex1 ex2 ex3
迈克 78.0 89.0 89.0
萨拉 98.0 78.0 65.0
大卫 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()

需要对代码进行一点修改,尽管代码在复杂性上效率不高

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()

暂无
暂无

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

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