简体   繁体   English

排序包含数字的列表

[英]Sorting Lists containing numbers

I have a series of lists like this that include scores.The first number following the name is student status. 我有一系列这样的列表,包括分数。名称后面的第一个数字是学生状态。 1=Freshman 2=Sophomore 3=Jr.4=Sr 1 =新生2 =大二3 = Jr.4 = Sr.

Ablao 3 74 96 72 88 71 80 83 77 90 88 95 71 76 94 80 74 98 77 
Anderson 3 76 92 98 95 92 76 93 97 85 76 85 93 82 88 75 84 92 77 
Aspinwall 1 86 74 78 97 86 94 73 95 74 91 75 94 83 99 83 78 88 96 
Bacon 4 72 95 81 80 89 88 100 87 87 81 79 77 75 83 87 96 72 95 

I need to create a function that will compute various statistics such as high, low mean of lab avg., program avg, etc based on the year of the student. 我需要创建一个函数,根据学生的年份计算各种统计数据,例如实验室平均值的高,低平均值,程序平均值等。 I already have functions created for averages. 我已经为平均值创建了函数。 I am just having trouble sorting the data by year of the student. 我只是在学生按年份排序数据时遇到问题。

So far I have, 到目前为止,我有,

 All_Years=[]

 Freshman=[]

 Sophomores=[]

 Juniors=[]

 Seniors=[]

def make_lists_of_status():
if (student_status==1):
    Freshman.append(student_scores)


elif (student_status==2):
    Sophomores.append(student_scores)


elif (student_status==3):
    Juniors.append(student_scores)



elif (student_status==4):
    Seniors.append(student_scores)



elif(student_status==1 or 2 or 3 or 4):
    All_Years.append(student_scores)





 def statistics_func():
     user_stat_choice='x'
     print("This option is used for viewing statistics sorted by the     year of the student")
     print("Please select one of the following options:")
     print("(a) for All Years, (b) for Freshman, (c) for Sophomores,   (d) for Juniors, (e) for Seniors")
     user_stat_choice=print(input("Enter your choice here:"))

    if(user_stat_choice=='a'):

    print ("Hi/Low/Mean of all weighted scores   is:",max(All_Years),min(All_Years),(sum(All_Years)/len(All_Years)))

    print ("Hi/Low/Mean of all lab averages is:")

    print ("Hi/Low/Mean of all program averages is:")

Here is a sample of how the output should be You have selected Statistics 以下是输出应该是如何选择统计数据的示例

This option is for viewing statistics sorted by the year of student.
Please select one of the following options: 
a for ALL YEARS
b for FRESHMAN
c for SOPHMORES
d for JUNIORS
e for SENIORS

Enter your choice here: a
For All Students:
High/Low/Mean of all Weighted Scores:  89.9 / 81.6 / 85.41883333333335
High/Low/Mean of all Lab Averages:  89.6 / 79.6 / 85.28333333333332
High/Low/Mean of all Program Averages:  98.33333333333333 /     71.66666666666667 / 85.90555555555554

Back to the Main Menu....
Ablao 3 74 96 72 88 71 80 83 77 90 88 95 71 76 94 80 74 98 77 
Anderson 3 76 92 98 95 92 76 93 97 85 76 85 93 82 88 75 84 92 77 
Aspinwall 1 86 74 78 97 86 94 73 95 74 91 75 94 83 99 83 78 88 96 
Bacon 4 72 95 81 80 89 88 100 87 87 81 79 77 75 83 87 96 72 95

To read this from a file: 要从文件中读取此内容:

f = open("path to file")
data = [a.strip('\n') for a in f.readlines()]

Now to sort it an example: 现在给它排序一个例子:

for i in data:
    n = i.split(" ")
    if n[1] == "1":
        for x in n[2:]:
            Freshman.append(int(x))

First I would recommend creating a student class 首先,我建议创建一个学生班

class Student(object):
    def __init__(self, name, status, *grades):
        self.name = name
        self.status = status
        self.grades = grades

   @property
   def student_year(self):
       return ('Freshman','Sophomore','Junior','Senior')[self.status-1]

This could easily be populated from a CSV file and stored in a list, All_Years . 这可以很容易地从CSV文件填充并存储在列表All_Years You could then get you list by 然后你可以让你列出

freshman = [student for student in All_Years if student.student_year = 'Freshman']

You could also skip the student_year things, but it does make the later coding easier to read. 你也可以跳过student_year东西,但它确实使后面的编码更容易阅读。

Consolidated grades could be done using the built-in module itertools by 可以使用内置模块itertools来完成合并等级

freshman_grades = list(itertools.chain(student.grades for student in freshman))

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

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