简体   繁体   English

Python从列表中获得平均值

[英]Python getting average from list

So yesterday I finished up the code I designed which ask the school administrator how many students you want to track, their respective grades and most importantly what courses they took. 因此,昨天我完成了我设计的代码,该代码询问学校管理员您想跟踪多少个学生,他们各自的成绩以及最重要的是他们修了什么课程。

Thus giving me a code: 因此给我一个代码:

COLS= int(input("number of students to enter: "))
ROWS= int(input("number of grades per student: "))
number =[]
for c in range(COLS):
    studentinfo=[]
    student =(input("enter student Name: "))
    studentinfo.append(student)

    for r in range (ROWS):
        course=input("Enter course Code: ")
        studentinfo.append(course)
        grades =float(input("Enter grade for module: "))
        studentinfo.append(grades)
    number.append(studentinfo)

print(number)

Which gives me a sample output of: 这给了我一个示例输出:

number of students to enter: 2
number of grades per student: 1
enter student Name: KenL
Enter course Code: MA344
Enter grade for module: 80
enter student Name: Harry
Enter course Code: PY101
Enter grade for module: 60
[['KenL', 'MA344', 80.0], ['Harry', 'PY101', 60.0]]

Now the idea is with that current output, I Want to create a function which would take the student list and course code and returns a new list which contains the names of students from the students list that have a grade higher than the average in course code. 现在的想法是使用当前输出,我想创建一个函数,该函数将获取学生列表和课程代码,并返回一个新列表,其中包含学生列表中成绩高于课程代码平均水平的学生姓名。

For example: above_avg(number,"MA22") returns a list of the names of students who perform better than average in MA22. 例如:above_avg(number,“ MA22”)返回在MA22中表现优于平均水平的学生的姓名列表。

I've started of by writting this code down: 我首先将这段代码写下来:

lookup=input("Which course code do you want to lookup: ")
def find_above_avg(number,lookup):
    if lk in number:
        avg=...

If anyone has any suggestions as to how I may alter the code I have so I can perform the look-up it would be greatly appreciated. 如果有人对我如何更改代码有任何建议,以便可以执行查找,将不胜感激。

l = [['KenL', 'MA344', 80.0], ['Harry', 'PY101', 60.0]]

lookup=raw_input("Which course code do you want to lookup: ")
avg = 60
def find_above_avg(number):
    return [i for i in l if lookup in i and  i[2] > avg]

print find_above_avg(avg)

Output: 输出:

Which course code do you want to lookup: MA344
[['KenL', 'MA344', 80.0]]

Well, you can make it work with lists. 好吧,您可以使其与列表一起使用。

First, we can try selecting students that took a given lecture: 首先,我们可以尝试选择参加给定讲座的学生:

def select_by_course(list_of_students, course):
    return [student for student in list_of_students if student[1] == course]

It's also easy to find the average: 找到平均值也很容易:

def average_grade(list_of_students, course):
    grades = [grade for (name, code, grade) in list_of_students if code == course]
    return sum(grades) / len(grades)

Then, we realize that it's not too difficult to do what you want: 然后,我们意识到做您想要的事并不难:

def find_above_average(list_of_students, course):
    relevant_students = select_by_course(list_of_students, course)
    average = average_grade(relevant_students, course)
    return [student for student in relevant_students if student[2] > average]

Here, we're doing it in three loops (one on the whole data and two on the relevant students), you can do it in two if you want (try it). 在这里,我们分三个循环进行(一个循环处理整个数据,两个循环处理相关的学生),如果需要,您可以分两个循环进行(尝试)。

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

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