Need help understanding why this value is staying as 1? Python CSV -


so block of code supposed open csv file, values column 1-3 (not 0). once has got values each row , 3 columns, supposed add these values , divide 3. thought code work addition of 3 columns in each row doesn't seem working. if tell me why , how can fix this, great, thank you. i'm pretty problem lies @ for index, summedvalue in enumerate (sums): specifically, "summedvalue" value.

if order ==("average score"):     askclass = str(input("what class?"))     if askclass == ('1'):             open("class1.csv") f:                 columns = f.readline().strip().split(" ")                 sums = [1] * len(columns)                  line in f:                     # skip empty lines                     if not line.strip():                          continue                      values = line.split(" ")                     in range(1,len(values)):                         sums[i] += int(values[i])                  index, summedvalues in enumerate (sums):                       print (columns[index], 1.0 * (summedvalues) / 3) 

from statistics import mean import csv  open("class1.csv") f:     # create reader object     r = csv.reader(f)     # skip headers     headers = next(r)     # exract name row , use statistics.mean average row[1..     # mapping scores ints     avgs = ((row[0], mean(map(int, row[1:]))) row in r)     # unpack name , average , print     name, avg in avgs:         print(name,avg) 

unless have written empty lines csv file there won't any, not sure how header fits can use if necessary.

you can unpack * syntax in python 3 think bit nicer:

avgs = ((name, mean(map(int, row))) name, *row in r) name, avg in avgs:     print(name,avg) 

to order sort average using reverse=true sort highest lowest:

from statistics import mean import csv operator import itemgetter  open("class1.csv") f:     r = csv.reader(f)     avgs = sorted(((name, mean(map(int, row))) name, *row in r),key=itemgetter(1),reverse=true)     name, avg in avgs:         print(name,avg) 

passing key=itemgetter(1) means sort second subelement average in each tuple.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -