我是一名中学教师,他试图找到一种合适的方法来教授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? "))
对于如何简化此计划的任何建议都将非常感激。