Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 我需要一些代码,让我找到我的.txt文件中的最高分数_Sorting_Python 3.x - Fatal编程技术网

Sorting 我需要一些代码,让我找到我的.txt文件中的最高分数

Sorting 我需要一些代码,让我找到我的.txt文件中的最高分数,sorting,python-3.x,Sorting,Python 3.x,还有,在我的平均水平下,有时它会打印太多的名字。 我有这段代码,但正在努力在我的文本文件中显示从最低到最高的分数。它看起来是这样的: 名称,1 名称,4 姓名,7 姓名,9 姓名,10 这是我的代码: import sys Viewclassnum= input ('Which class do you want to view 1,2 or 3?') print('please input a, b or c') print('a)Alphabeically') print('b)aver

还有,在我的平均水平下,有时它会打印太多的名字。 我有这段代码,但正在努力在我的文本文件中显示从最低到最高的分数。它看起来是这样的:
名称,1
名称,4
姓名,7
姓名,9
姓名,10

这是我的代码:

import sys

Viewclassnum=  input ('Which class do you want to view 1,2 or 3?')
print('please input a, b or c')
print('a)Alphabeically')
print('b)average')
print('c)highest to lowest')
Viewclasssort= input ('how would you like to view it:')



                                                                                                    #setting variable fd to open the file set to File_name (from when the program asked the user for  

if Viewclassnum=='1' and Viewclasssort=='a':
    with open('class1.txt', 'r') as r:
        for line in sorted(r):
             print(line, end='')
    Again=input('Do you want to view another class yes or no?')
    if Again=='yes':
        Viewclassnum=  input ('Which class do you want to view 1,2 or 3?')
        print('please input a, b or c')
        print('a)Alphabeically')
        print('b)average')
        print('c)highest to lowest')
        Viewclasssort= input ('how would you like to view it:')
    if Again=='no':
        print('Bye')


if Viewclassnum=='1' and Viewclasssort=='b':
  fd = open('class1.txt')
  lines = [line.strip() for line in fd]
  f = {}
  for line in lines:
    split = [i for i in line.split(',')]
    key = split[0]
    f[key] = [int(n) for n in split[1:]]
    avg_mark = lambda name:sum(f[name])/len(f[name])
    for i in sorted(f.keys(),key=avg_mark,reverse=True):
        print (i,avg_mark(i),"\n")
  Again=input('Do you want to view another class yes or no?')
  if Again=='yes':
        Viewclassnum=  input ('Which class do you want to view 1,2 or 3?')
        print('please input a, b or c')
        print('a)Alphabeically')
        print('b)average')
        print('c)highest to lowest')
        Viewclasssort= input ('how would you like to view it:')
  if Again=='no':
        print('Bye')
输出为:

Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:a
name,3
name,8
name,9
2name,4
2name,7
2name,8
Do you want to view another class yes or no?yes
Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:b
name 3.0 

name 8.0 

name 9.0 

2name 9.0 

2name 4.0 

name 9.0 (repeated don't know why)

name 7.0 
我还想将所有名称组合在一起,使其看起来像这样:

名称,3,5,6
2名称,4,5,6

只显示最近的三个分数。 对于从最高到最低的分数,我希望得到以下结果: 姓名,10 2姓名,8 5姓名,4 3姓名,5


谢谢

以下是我认为我会如何处理这件事。因为字典是不排序的,所以它们不能被排序,所以我列出了一个学生对象列表,它是可排序的

我还减少了大量重复3次的输入代码

import sys

class Student:
    def __init__(self, name, marks):
        self.name = name
       self.marks = marks
    def avg_mark(self):
        return sum(self.marks)/len(self.marks)
    def __str__(self):
        return '{0}: {1}, {2}'.format(self.name, self.marks, self.avg_mark())

def load_grades(filename):
    fd = open(filename, 'r')
    f = {}
    for line in fd:
        split = line.split(',')
        key = split[0]
        marks = [int(n) for n in split[1:]]
        try:
           f[key] += marks
        except KeyError:
            f[key] = marks
    students = []
    for key, values in f.items():
        students.append(Student(key, values[-3:]))
    return students

def sort_by_name(students):
    def get_name(student):
    return student.name
    students = sorted(students, key=get_name)
    return students

def sort_by_avg(students):
    def get_avg(student):
        return student.avg_mark()
    students = sorted(students, key=get_avg)
    return students


again = True

while again:
    class_num =  input ('Which class do you want to view 1,2 or 3?')
    print('please input a, b or c')
    print('a)Alphabeically')
    print('b)average')
    print('c)highest to lowest')
    sort = input ('how would you like to view it:')

    if class_num =="1" and sort =="a":
        students = load_grades('class1.txt')
        students = sort_by_name(students)
    elif class_num =="1" and sort =="b":
        students = load_grades('class1.txt')
        students = sort_by_avg(students)

    for student in students:
        print(student)


    Again=input('Do you want to view another class yes or no?')
    again = (Again=='yes')

print('bye')
示例输出:

Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:a
2name: [4, 7, 8, 69], 22.0
name: [3, 8, 9], 6.666666666666667
Do you want to view another class yes or no?yes 
Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:b
name: [3, 8, 9], 6.666666666666667
2name: [4, 7, 8, 69], 22.0
Do you want to view another class yes or no?no
bye

你能告诉我们你得到了什么输出以及你得到了什么输出吗?这将帮助我们更好地理解你的问题。现在就做。请询问您是否需要其他内容查看
name
的所有实例都是同一个人?name是同一个人,而2name是同一个人。输出是什么样子的?输出是由学生格式化的。uu str_uuu()方法是否需要替换文件名如果您查看while循环,我将调用load_grades()函数的参数为
class1.txt
。现在,您可以使用不同的文件名重用该函数。回溯(最近一次调用):文件“C:\Users\Aaron\Documents\python CA\Average trail.py”,第59行,在打印(学生)文件“C:\Users\Aaron\Documents\python CA\Average trail.py”的第10行,str返回“{0}:{1},{2}”。格式(self.name,self.marks,self.avg_mark())文件“C:\Users\Aaron\Documents\python CA\Average trail.py”,第8行,在平均标记返回和(self.marks)/len(self.marks)ZeroDivisionError:division by zero我该怎么做