'Find average of every line in list. (open from .txt file)

def day_average(daylist):
lines_list = open('lab03/pollutiondaily.txt').readlines()
for val in lines_list: 

    daylist=[] 
    for i in val.split(','): 

        daylist+=[int(i)] 
    
    print("Day", "average: " "{:.2f}".format (sum(daylist)/len(daylist)))

def main():
print("The pollution levels for the week are:")
print ("Day 1 average: ", day_average(1))
print ("Day 2 average: ", day_average(2))
print ("Day 3 average: ", day_average(3))
print ("Day 4 average: ", day_average(4))
print ("Day 5 average: ", day_average(5))
print ("Day 6 average: ", day_average(6))
print ("Day 7 average: ", day_average(7))
main()

This is what I got so far.

The exact question is "Write a program to extract data from a file (pollutiondaily.txt). Each line of the file contains a set of numbers of the pollution levels for a particular day (one reading per hour, so 24 readings per day). Create a function called day_average(daylist) that accepts the list as a parameter and returns a new list of daily averages." The goal is to make the list and find the average, and to make function in function. We wrote the day_average(daylist) to call it later with print "Day 1 average is: ", day_average(1) to print day_average(7) in def main()

The txt file looks like this (each line means each day):

32,42,32,51,43,64,58,50,46,78,41,32,41,32,29,39,65,37,31,59,52,23,27,63
91,93,44,31,55,42,38,54,46,48,61,45,42,42,129,139,45,47,51,49,43,49,27,23
67,54,83,45,56,98,92,121,153,115,98,80,59,63,84,121,144,119,103,97,83,75,56,64
132,142,154,151,143,164,158,150,146,78,141,32,141,142,112,113,116,123,123,119,152,123,127,163
89,78,67,65,56,98,123,144,179,86,82,90,172,124,156,187,122,154,144,165,87,152,93,157
76,92,154,61,147,154,68,154,66,88,91,132,41,32,129,49,65,88,61,159,182,123,127,53
64,29,54,43,54,27,54,76,61,39,44,61,54,112,110,91,94,97,92,76,88,65,61,60


Solution 1:[1]

With the csv and statistics modules you can do this in three simple lines of code:

import csv
import statistics

with open("pollutiondaily.txt") as file:
    for row in csv.reader(file):
        print(statistics.mean(map(int, row)))

With your example pollutiondaily.txt file this produces:

44.458333333333336
55.583333333333336
88.75
131.04166666666666
119.58333333333333
99.66666666666667
66.91666666666667

Solution 2:[2]

print("The pollution levels for the week are:")

def day_average(day):
    lines_list = open('lab03/pollutiondaily.txt').readlines()
    Week_record=[] # Creating a whhole week record
    for val in lines_list:
        N=[] # Creating list in list
        for i in val.split(','):
            N+=[int(i)] # Converting string in list to integer
        Week_record+=[N]
    print(sum(Week_record[day])/len(Week_record[day])) # Finding the average of each list in created list by slicing the week
day=3
day_average(day)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samwise
Solution 2