我正在创建一个打开并读取csv文件的程序,并按以下方式排序:
该计划应存储每位学生的最后3个分数。这是我坚持并需要帮助的部分。按字母顺序对文件进行排序时,程序需要查看每个学生最近3个最近的分数并选择最高分数。目前,我的代码只按字母顺序对文件进行排序。它会查看最近的3个分数并选择最高分。这是我需要帮助的地方。
我的代码已经将分数从最高到最低排序,但它会打印出每个学生获得的所有分数,而不是从最近的3分中打出最高分。
Andrew 1
Andrew 2
Andrew 3
Andrew 4
Andrew 5
最后,我需要帮助计算每个学生的平均分数。我猜测它应该做的方式是,加上安德鲁的最后3分,分别是5分,4分和3分,除以3分。
这是我的代码:
import csv, operator
selected_class = input("Pick a class file, (5, 6 or 7)? ")
print("1. Alphabetical order.")
print("2. Highest to lowest.")
print("3. Average score.")
selected_sorting = input("Pick an option 1, 2, or 3: ")
class_file = "Class " + selected_class + ".csv"
open_file = open(class_file)
csv_file = csv.reader(open_file)
if selected_sorting == "1":
sorted_name = sorted(csv_file, key=operator.itemgetter(0))
for i in sorted_name:
print(i)
elif selected_sorting == "2":
sorted_results = sorted(csv_file, key=lambda row: int(row[1]), reverse=True)
for i in sorted_results:
print(i)
elif selected_sorting == "3":
答案 0 :(得分:1)
我将提供一些演示代码:
# -*- coding: utf-8 -*-
import csv
from collections import defaultdict
from statistics import mean
class_file = 'scores.csv'
open_file = open(class_file)
csv_file = csv.reader(open_file)
def main():
# First, use student name to group by all scores, this will
# generate structure like this:
# {
# 'Andrew': [1, 2, 3, 4, 5]),
# 'Luck': [10, 20]),
# }
score_groups = defaultdict(list)
for name, score in csv_file:
score_groups[name].append(int(score))
# Secondary, use the 3 latest socres only
l3_score_groups = [(key, value[-3:]) for key, value in score_groups.items()]
print('1. Alphabetical order with each students highest score.')
l3_highest_score_groups = [(key, max(values)) for key, values in l3_score_groups]
for name, score in sorted(l3_highest_score_groups, key=lambda x: x[0]):
print(name, score)
print('2. By the highest score, highest to lowest.')
l3_highest_score_groups = [(key, max(values)) for key, values in l3_score_groups]
for name, score in sorted(l3_highest_score_groups, key=lambda x: x[1], reverse=True):
print(name, score)
print('3. Average score, highest to lowest.')
l3_aver_score_groups = [(key, mean(values)) for key, values in l3_score_groups]
for name, score in sorted(l3_aver_score_groups, key=lambda x: x[1], reverse=True):
print(name, score)
if __name__ == '__main__':
main()
以上是上面使用的技术:
collections.defaultdict
:进行数据分组工作时的有用数据结构。list-comprehensions
:用于更改/过滤可迭代数据的强大工具。statistics.mean
:计算列表的平均值。希望它有所帮助。
答案 1 :(得分:0)
我可以建议你看看大熊猫(它是anacondas发行的一部分)
import pandas as pd
dataframe = pd.read_csv(' your file ')
print dataframe.columns
student1 = dataframe[dataframe['studentnamecolumn']=='Andrew']
last3 = student1.sort('examdatecolumnname').iloc[-3:]
avgscore = last3['examscorecolumn'].mean()
通过上述组合,您应该能够做大部分事情。为了帮助我建议阅读DataAnalysis for Python,它解释了很多这个