如何在python中使用可以支持不同大小数组的2D数组?



使用python程序查找下列所有学生各科平均成绩:

Student ID  | Quiz   | Research Project | Assignment     | Final Exam                   
1           | 40/50  | 50/100           | 15/20          | 50/100
2           | 50/50  | 30/100           | 10/20          | 10/100
3           | 22/50  | 43/100           | 8/20           | 30/100
4           | 40/50  | 36/100           | 19/20          | 43/100
5           | 20/50  | 80/100           | 11/20          | 51/100

要遵循的规则:

  1. 使用二维数组存储以上数据(必须是浮点数)并进行计算。确保平均值以小数点后2位的形式打印。(在数组中不包含学生ID和header)
  2. 用变量标记存储你的2D数组
  3. 确保你的算法能够支持不同大小的数组

示例输出应该完全像这样:

student 1 have 63.75 average.
student 2 have 47.50 average.
student 3 have 39.25 average.
student 4 have 63.50 average.
student 5 have 56.50 average.

我试着编码它,下面是我的代码:

markss = [[0.8, 0.5, 0.75, 0.5], [1, 0.3, 0.5, 0.1], [0.44, 0.43, 0.4, 
0.3], [0.8, 0.8, 0.95, 0.43], [0.4, 0.8, 0.55, 0.51]]

average = [(sum(avg)/float(len(avg)))*100 for avg in markss]
print(f"student 1 have {average[0]:.2f} average.")
print(f"student 2 have {average[1]:.2f} average.")
print(f"student 3 have {average[2]:.2f} average.")
print(f"student 4 have {average[3]:.2f} average.")
print(f"student 5 have {average[4]:.2f} average.")
my2dlist = []           # declare normal list
my2dlist.append([x for x in range(2)])   # adding lists
my2dlist.append([x for x in range(9)])   
print(my2dlist)
print(my2dlist[0][1])   # accessing entry

相关内容

  • 没有找到相关文章

最新更新