简体   繁体   English

如何使用 Python 函数对嵌套列表中的每个列表求和?

[英]How can I sum each list within a nested list using a Python function?

I have a random set of test scores for 50 students.我有一组随机的 50 名学生的考试成绩。 Each student answered 20 questions, and the maximum score for each question is 5. How can I get the total score of each student in a list by using a Python function?每个学生回答了20道题,每道题的最高分是5。如何使用Python函数获取列表中每个学生的总分?

results_sheet = []
num_of_questions = 20
num_students = 50


#generate scores for each student and append to results_sheet

for student in range(num_students):
        scores = random.choices(list(range(0,6)),k=num_of_questions)
        results_sheet.append(scores)
        
#The result here is a list of lists.

So if I call the results for 2 students, I will get the following:因此,如果我调用 2 个学生的结果,我将得到以下结果:

print(results_sheet[0:2])
[[4, 2, 3, 4, 2, 0, 0, 1, 2, 4, 0, 3, 5, 4, 3, 2, 5, 0, 3, 3], [5, 1, 1, 3, 2, 0, 2, 5, 3, 1, 1, 3, 5, 1, 4, 3, 2, 4, 5, 4]]

I have tried the following code but am missing something.我尝试了以下代码,但缺少一些东西。 Or perhaps the code is simply wrong:或者代码可能完全错误:

def sum_score(results_sheet):

total = 0
for val in results_sheet:
    total = total + val
return total  

The end result should look something like this for 2 students:对于 2 个学生,最终结果应该是这样的:

total_scores = [47, 55]

You can easily use numpy.sum, and set axis to negative.您可以轻松使用 numpy.sum,并将轴设置为负。

import random
import numpy

results_sheet = []
num_of_questions = 20
num_students = 50


#generate scores for each student and append to results_sheet

for student in range(num_students):
        scores = random.choices(list(range(0,6)),k=num_of_questions)
        results_sheet.append(scores)

If you want the sum of all:如果你想要所有的总和:

numpy.sum(results_sheet, axis=-1)

array([43, 44, 61, 64, 52, 44, 48, 51, 36, 50, 51, 45, 56, 47, 53, 60, 53,
       45, 45, 46, 53, 54, 39, 42, 54, 67, 44, 48, 51, 58, 53, 54, 40, 52,
       62, 54, 61, 58, 47, 38, 51, 45, 50, 49, 61, 30, 50, 59, 54, 34])

So if I call the results for 2 students:因此,如果我调用 2 个学生的结果:

numpy.sum(results_sheet, axis=-1)[0:2]
array([43, 44])

First, if you are having a return statement outside of a function it will not work.首先,如果您在函数之外有一个return语句,它将不起作用。 Secondly, you can also just use the build-in sum function in Python:其次,您也可以只使用 Python 中的内置sum函数:

sum_of_all = sum(scores)

Then you can do this for each individual student's scores:然后您可以为每个学生的分数执行此操作:

results_sheet = []
num_of_questions = 20
num_students = 50


#generate scores for each student and append to results_sheet

for student in range(num_students):
        scores = random.choices(list(range(0,6)),k=num_of_questions)
        results_sheet.append(scores)

sums = [sum(result_sheet) for result_sheet in results_sheet]

The sums variable will be a list of summed scores for the students, in the same order as the generated scores. sums变量将是学生的总分列表,其顺序与生成的分数相同。

Try a list comprehension:尝试列表理解:

final = [sum(lst) for lst in results_sheet]

Or use map :或使用map

final = list(map(sum, results_sheet))

Edit:编辑:

To use a function:要使用函数:

def func(results_sheet):
    return [sum(lst) for lst in results_sheet]
print(func(results_sheet))

You have to apply sum() on every 2nd level list, like below您必须在每个第二级列表上应用 sum(),如下所示

import random

results_sheet = []
num_of_questions = 20
num_students = 50


#generate scores for each student and append to results_sheet

for student in range(num_students):
        scores = random.choices(list(range(0,6)),k=num_of_questions)
        results_sheet.append(scores)


total = 0
for val in results_sheet:
    total = total + sum(val)
print(total)

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

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