Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 在Python中对CSV文件中的多个列进行排序_Sorting_Csv_Python 3.x - Fatal编程技术网

Sorting 在Python中对CSV文件中的多个列进行排序

Sorting 在Python中对CSV文件中的多个列进行排序,sorting,csv,python-3.x,Sorting,Csv,Python 3.x,我是一名中学教师,试图找到一种合适的方法来教授KS4学生一些技术,使他们能够将数据写入CSV文件,然后从文件中读取相同的数据,并以有组织的结构用Python显示。 学生们和我对如何将数据写入文件有着清晰的理解,但是从将数据从文件中获取到Python中,然后对其进行排序的角度来看,解释起来变得相当棘手和复杂 我已经创建了一个程序,允许用户输入他们的名字,后跟3个独立的数字,所有这些数字都会以如下所示的格式写入CSV文件 James , 3 , 7 , 4 David , 5 , 5 , 9 Ste

我是一名中学教师,试图找到一种合适的方法来教授KS4学生一些技术,使他们能够将数据写入CSV文件,然后从文件中读取相同的数据,并以有组织的结构用Python显示。 学生们和我对如何将数据写入文件有着清晰的理解,但是从将数据从文件中获取到Python中,然后对其进行排序的角度来看,解释起来变得相当棘手和复杂

我已经创建了一个程序,允许用户输入他们的名字,后跟3个独立的数字,所有这些数字都会以如下所示的格式写入CSV文件

James , 3 , 7 , 4
David , 5 , 5 , 9
Steven , 8 , 3 , 9
这些结果保存在文件“7G1.csv”中

到目前为止,我有一个功能正常的程序,按字母顺序排序,从高到低,从平均值从高到低。我已经能够通过研究和从众多来源拼凑技术来建立以下计划,但有谁能提出一个16岁儿童可以理解的更简单、更有效的方法

import csv

G1 = open('7G1.csv')
csv_G1 = csv.reader(G1)
list7G1 = []
for column in csv_G1:
    column[1] = int(column[1])
    column[2] = int(column[2])
    column[3] = int(column[3]) 
    minimum = min(column[1:4])
    column.append(minimum)
    maximum = max(column[1:4])
    column.append(maximum)
    average = round(sum(column[1:4])/3)
    column.append(average)
    list7G1.append(column[0:7])

group_menu = 0
while group_menu != 4:
    group_menu = int(input("Which group / class do you want to look at?\n1.7G1?\n2.7G2?\n3.7G3?\n4.Quit? "))                     
    if group_menu == 1:
        print ("You have chosen to focus on group 7G1.")
        menu = int(input("\nDo you want to...\n1.Sort Alphabetically?\n2.Sort Highest to Lowest?\n3.Sort Average Highest to Lowest?\n4.Exit Group? "))
        while menu != 4:
            if menu == 1:
                print("You have chosen to Sort Alphabetically...")
                namesList = [[x[0], x[5]] for x in list7G1]
                print("\nSorted Alphabetically with Highest Scores \n")
                for names in sorted(namesList):
                    print (names)
            elif menu == 2:
                print("You have chosen to Sort Highest to Lowest...")
                highestScore = [[x[5], x[0]] for x in list7G1]
                print("\nScores Highest to Lowest \n")
                for hightolow in sorted(highestScore, reverse = True):
                    print (hightolow)
            elif menu == 3:
                print("You have chosen to Sort Average Highest to Lowest")
                averageScore = [[x[6], x[0]] for x in list7G1]
                print("\nAverage Scores \n")
                for average in sorted(averageScore, reverse = True):
                    print(average)
            elif menu == 4:
                print("You have chosen to exit this group")
            else:
                print("This is not a valid option")
            menu = int(input("\nDo you want to...\n1.Sort Alphabetically?\n2.Sort Highest to Lowest?\n3.Sort Average Highest to Lowest?\n4.Exit Group? "))
任何关于如何简化该计划的建议都将不胜感激