从最高到最低排序数据,并从文本文件中按Python从最高到最低排序平均值

时间:2015-05-06 14:47:55

标签: python sorting

我需要创建一个程序,键入你想要的类和你想要它的内容(按字母顺序,平均值,从最高到最低),并在运行时打印它。到目前为止,这是我的代码,我已经设法按字母顺序排列,但我在平均而且从最高到最低的部分都在苦苦挣扎。

到目前为止,这是我的代码的一部分:

def sortchoice():
      sort = input("What class do you want to sort? 1, 2, 3? if you dont want to sort any, type: 0  ")

if sort == "1":  

    def viewclasschoice1():
        viewclass = input("choose either alphabetically (type a), average (type b) or highest (type c)?")
        if viewclass == "a":
            with open('Class 1.txt', 'r') as r:
                for line in sorted(r):
                    print(line, end='')


        elif viewclass == "b":
             with open('Class 1.txt', 'r') as r:
                for line in sorted(r):
                    print(line, end='')


        elif viewclass == "c":
            def score(line):
                return int(line.split(',')[1])

            with open('Class 1.txt', 'r') as r:
                for line in sorted(r, key=score, reverse=True):
                    print(line)    

        else:
            viewclasschoice1()
    viewclasschoice1()    

1 个答案:

答案 0 :(得分:0)

为最高点,只需使用

sorted(r,reverse=True)

平均值只是这样做

import numpy
print numpy.mean(r)
相关问题