【问题标题】:Sorting Lists containing numbers排序包含数字的列表
【发布时间】:2016-05-25 21:53:31
【问题描述】:

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

【问题讨论】:

  • “按学生年份排序数据”到底是什么意思?如果您可以添加示例输入和输出,那就太好了。
  • 刚刚添加了一个示例@VedangMehta
  • 另外,您的All_Years 列表将为空,因为永远不可能到达最后一个elif 语句

标签: python function sorting statistics


【解决方案1】:
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))

【讨论】:

    【解决方案2】:

    首先我建议创建一个学生类

    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 by

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

    【讨论】:

      猜你喜欢
      • 2020-04-14
      • 2020-10-31
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多