排序包含数字的列表

时间:2016-05-25 13:18:16

标签: python function sorting statistics

我有一系列这样的列表,其中包括得分。名称后面的第一个数字是学生状态。 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 

我需要创建一个功能,根据学生的年份计算各种统计数据,例如实验室平均值的高,低平均值,程序平均值等。我已经为平均值创建了函数。我只是在学生按年份排序数据时遇到问题。

到目前为止,我有,

 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:")

以下是输出应该如何的示例     您已选择统计

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....

2 个答案:

答案 0 :(得分:0)

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

要从文件中读取此内容:

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

现在给它排序一个例子:

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

答案 1 :(得分:0)

首先,我建议创建一个学生班

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]

这可以很容易地从CSV文件填充并存储在列表All_Years中。然后你可以通过

列出你的名单
freshman = [student for student in All_Years if student.student_year = 'Freshman']

您也可以跳过student_year内容,但它确实使后面的代码更易于阅读。

合并成绩可以使用

内置模块itertools完成
freshman_grades = list(itertools.chain(student.grades for student in freshman))