Finding Maximum Value in CSV File

后端 未结 5 2050
醉梦人生
醉梦人生 2021-01-29 06:14

Have an assignment of finding average and maximum rainfall in file \"BoulderWeatherData.csv\". Have found the average using this code:

    rain = open(\"BoulderW         


        
5条回答
  •  萌比男神i
    2021-01-29 07:19

    You're already accumulating total across loop iterations.

    To keep track of a maxvalue, it's basically the same thing, except instead of adding you're maxing:

    total = 0
    maxvalue = 0
    
    for line in data:
        r = line.split(",")
        value = float(r[4])
        total = total + value
        maxvalue = max(maxvalue, value)
    
    print(total)
    print(maxvalue)
    

    Or, if you don't want to use the max function:

    for line in data:
        r = line.split(",")
        value = float(r[4])
        total = total + value
        if value > maxvalue:
            maxvalue = value
    

提交回复
热议问题