需要帮助排序数据和计算平均值

时间:2016-05-27 03:56:48

标签: python list file sorting

我需要创建一个在执行时会像这样运行的代码:

What would you like to do: s

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: e

For Seniors:
High/Low/Mean of all Weighted Scores:  89.53999999999999 /  81.60000000000001 / 86.15736842105264
High/Low/Mean of all Lab Averages:  89.6 / 80.2 / 86.21052631578948
High/Low/Mean of all Program Averages:  94.33333333333333 / 77.0 / 86.78947368421052

它应该从文件中获取信息并按等级分开分数以便计算。我如何按学生的成绩分开数据来计算平均值?这里完全无能为力。

名称后面的第一个数字是成绩

   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 

我到目前为止编写的代码

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



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

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]

for dat1,dat2,dat3 in zip (ablao,anderson,aspinwall):
    if dat1 == dat2:
        secondGrade1 = ablao.index(dat1)+1
        secondGrade2 = anderson.index(dat2)+1
        break
    if dat1 == dat3:
        secondGrade1 = ablao.index(dat1)+1
        secondGrade3 = aspinwall.index(dat3)+1
        break
    if dat2 == dat3:
        secondGrade2 = anderson.index(dat2)+1
        secondGrade3 = aspinwall.index(dat3)+1
        break
try:
    print (ablao[secondGrade1])
    print (anderson[secondGrade2])
    print (aspinwall[secondGrade3])
except:
    pass

将它们放入列表中,然后使用zip()检查是否符合其第一个元素。如果是,则定义变量的索引并打破循环。

输出;

>>> 
74 #only ablao's and anderson's first elements are same
76 #
>>> 

之后只是基本数学(计算平均值等)

答案 1 :(得分:0)

要阅读学生信息文件

with open('/path/to/scores.txt', 'r') as f:
    lines = f.readlines()

然后,您可以遍历从文件中读取的所有行并提取相关信息。 line.split()将一行划分为空格字符处的字符串列表,name, grade, *scores表示法使用Python 3功能优雅地将该列表解压缩为单个变量。下一行是将分数从一个字符串列表转换为一个整数列表。

for line in lines:
    name, grade, *scores = line.split()
    scores = list(map(int, scores))

    if grade == '1':
        Freshman.append(scores)
    elif grad == '2':
        Sophomores.append(scores)
    ...

从那里你应该能够使用你已经写过的代码。

PS:由于Python 2中的解包符号在该表单中不可用,如果您使用的是该版本,则可以使用以下内容:

for line in lines:
    split_line = line.split()
    name = split_line[0]
    grade = split_line[1]

    scores = map(int, split_line[2:])
    ...