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

时间:2022-10-21 15:43:34

I need to create a program that you type in which class you want and what you want it sorted in (alphabetically, averages and highest to lowest) and it prints it when run. This is my code so far I have managed to do the alphabetical part but am struggling on the average and the highest to lowest part.

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

Here is part of my code so far:

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


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 个解决方案

#1


for the Highest to lower, just use

对于最高点,只需使用

sorted(r,reverse=True)

for the average just do this

对于平均而言,这样做

import numpy
print numpy.mean(r)

#1


for the Highest to lower, just use

对于最高点,只需使用

sorted(r,reverse=True)

for the average just do this

对于平均而言,这样做

import numpy
print numpy.mean(r)