I have found all the values and everything and I tried sort, but it sorts separately rather than together. I want to sort by years, and grades should just follow the values of years. However, when I do sort(years), it would only sort years, leaving grades as it is.
when opening file, it would give me something like:
Year,Grade
2000,84
2001,34
2002,82
2012,74
2008,90
and so forth. So I have calculated average and everything.
years, average_grades = [],[]
avg = []
d = {}
with open(file,'r') as f:
next(f)
for line in f:
year, grade = (s.strip() for s in line.split(','))
if year in d:
d[year][0] += int(grade)
d[year][1] += 1
else:
d[year] = [int(grade),1]
for year, grades in d.items():
years.append(str(year))
average_grades.append(float(grades[0]) / grades[1])
return years, average_grades
Without sort, it would give me similar to this:
2001 74.625
2006 72.241
2012 70.875
2017 69.1981
2005 72.5
2008 71.244
2014 73.318
2004 72.1
2007 72.88
2000 73.1
With years.sort(), it would give me similar to this:
2000 74.625
2001 72.241
2002 70.875
2003 69.1981
2004 72.5
2005 71.244
2006 73.318
2007 72.1
So the sort will only work for years, but won't do that for grades. This problem has been bugging me for so long time now. I am not planning to use pandas.
yearsandaverage_gradesare not related when you return them.