Help_Python_Pls Help_Python_Pls - 7 months ago 8
Python Question

Python: Trying to create two functions. One iterates through the columns of a 2d list, while the other goes through the rows

def average_hour (temps, hour):
i = 0
for i in temps[None][i]:
total = total + temps[None][i]
average = total/(i+1)
return average

def average_day (temps, day):
i = 0
for i in temps[i][None]:
total = total + temps[i][None]
average = total/(i+1)
return average

def main():
temps = [
[73.0, 73.9, 73.0, 73.9, 73.9, 73.9, 73.0, 71.1, 71.1, 71.1, 70.0, 70.0, 70.0, 70.0, 70.0, 71.1, 71.1, 71.6, 71.1, 71.1, 70.0, 71.1, 70.0, 71.1],
[63.0, 63.0, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1, 61.0, 62.6, 62.6, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1, 62.1],
[68.0, 68.0, 69.1, 69.1, 69.1, 73.9, 75.0, 75.9, 81.0, 81.0, 82.0, 84.0, 80.1, 78.1, 75.9, 77.0, 80.1, 81.0, 82.0, 81.0, 81.0, 79.0, 78.1, 78.1],
[78.1, 78.1, 78.1, 77.0, 77.0, 77.0, 75.9, 75.0, 75.2, 75.9, 73.9, 73.9, 73.9, 73.0, 73.0, 73.0, 73.9, 73.9, 73.0, 73.4, 73.0, 73.0, 75.0, 75.0],
[71.1, 71.1, 70.0, 70.0, 70.0, 70.0, 70.0, 70.0, 70.0, 70.0, 69.1, 69.1, 69.1, 69.1, 68.0, 66.9, 66.0, 66.0, 66.0, 66.0, 66.0, 66.0, 64.9, 64.9],
[61.0, 61.0, 61.0, 61.0, 61.0, 61.0, 60.1, 60.1, 60.1, 60.1, 60.1, 60.1, 60.1, 60.1, 60.1, 60.1, 60.1, 60.1, 60.1, 61.0, 61.0, 61.0, 64.9, 64.9],
[57.0, 55.0, 55.0, 55.0, 54.0, 54.0, 55.0, 54.0, 55.0, 55.9, 55.9, 55.0, 55.0, 57.0, 62.1, 66.9, 71.1, 73.0, 75.0, 77.0, 78.1, 78.1, 78.1, 75.9],
[62.1, 61.0, 61.0, 60.1, 60.1, 59.0, 59.0, 61.0, 66.9, 72.0, 75.9, 78.1, 78.1, 78.1, 78.1, 78.1, 77.0, 75.0, 72.0, 71.1, 70.0, 68.0, 66.0, 64.0],
[63.0, 62.1, 61.0, 61.0, 59.0, 60.1, 59.0, 60.1, 66.9, 73.9, 75.9, 78.1, 81.0, 79.0, 81.0, 79.0, 79.0, 75.9, 73.0, 72.0, 71.1, 71.1, 71.1, 70.0],
[70.0, 69.1, 70.0, 69.1, 69.1, 69.1, 69.1, 69.1, 68.0, 68.0, 68.0, 70.0, 69.8, 70.0, 70.0, 70.0, 71.1, 70.0, 70.0, 71.1, 71.1, 71.1, 73.0, 73.0],
[61.0, 61.0, 60.1, 59.0, 59.0, 59.0, 57.9, 59.0, 59.0, 60.1, 61.0, 61.0, 63.0, 66.9, 66.0, 71.1, 71.1, 73.9, 72.0, 75.9, 75.0, 75.9, 75.9, 75.0],
[62.1, 62.1, 62.1, 61.0, 62.1, 61.0, 62.1, 62.1, 63.0, 62.1, 61.0, 61.0, 61.0, 61.0, 64.0, 64.9, 66.0, 66.0, 68.0, 70.0, 66.0, 64.0, 62.1, 62.1],
[63.0, 63.0, 64.0, 64.0, 64.9, 63.0, 63.0, 64.0, 68.0, 72.0, 75.0, 77.0, 79.0, 79.0, 79.0, 79.0, 78.1, 75.9, 73.0, 72.0, 71.1, 71.1, 70.0, 69.1],
[69.1, 68.0, 66.0, 66.0, 64.9, 64.0, 64.0, 63.0, 63.0, 64.0, 64.0, 64.0, 64.9, 68.0, 73.0, 75.0, 77.0, 79.0, 80.1, 80.1, 79.0, 77.0, 71.1, 64.0],
[57.9, 57.9, 59.0, 59.0, 57.0, 55.9, 55.0, 55.0, 60.1, 64.9, 70.0, 73.0, 73.0, 73.9, 75.0, 73.9, 75.0, 72.0, 61.0, 57.0, 55.9, 54.0, 53.1, 53.1],
[53.1, 53.1, 52.0, 52.0, 51.1, 51.1, 54.0, 55.0, 64.0, 69.1, 73.0, 77.0, 79.0, 81.0, 81.0, 81.0, 78.1, 75.0, 73.0, 72.0, 68.0, 66.9, 66.0, 66.0],
[62.1, 61.0, 57.9, 57.0, 57.0, 55.0, 54.0, 54.0, 57.0, 63.0, 66.0, 66.9, 70.0, 72.0, 72.0, 72.0, 71.1, 68.0, 63.0, 57.9, 59.0, 55.0, 55.0, 53.1],
[53.1, 51.1, 48.9, 48.0, 46.9, 45.0, 44.1, 45.0, 48.0, 51.1, 54.0, 55.0, 55.9, 57.9, 59.0, 60.1, 59.0, 57.0, 52.0, 46.9, 46.0, 43.0, 45.0, 42.1],
[42.1, 39.9, 39.9, 39.9, 39.9, 39.0, 37.9, 39.9, 45.0, 50.0, 55.0, 57.0, 59.0, 60.1, 61.0, 61.0, 61.0, 57.9, 52.0, 48.0, 46.9, 45.0, 45.0, 45.0],
[45.0, 43.0, 43.0, 43.0, 42.1, 42.1, 42.1, 43.0, 52.0, 57.0, 61.0, 64.0, 68.0, 70.0, 71.1, 70.0, 69.1, 66.9, 62.1, 57.0, 54.0, 54.0, 53.1, 52.0],
[51.1, 50.0, 50.0, 51.1, 51.1, 51.1, 50.0, 52.0, 57.9, 64.0, 70.0, 73.0, 75.0, 75.0, 75.9, 75.0, 73.0, 70.0, 66.9, 63.0, 62.1, 60.1, 60.8, 61.0],
[55.9, 57.0, 57.0, 57.0, 57.0, 57.0, 55.9, 55.0, 55.0, 55.0, 55.0, 55.0, 55.0, 54.0, 54.0, 54.0, 54.0, 54.0, 54.0, 54.0, 54.0, 54.0, 53.1, 53.1],
[57.0, 55.9, 55.0, 54.0, 54.0, 53.1, 53.1, 54.0, 54.0, 53.1, 54.0, 54.0, 54.0, 54.0, 54.0, 54.0, 54.0, 53.1, 53.1, 53.1, 62.1, 68.0, 73.0, 77.0],
[63.0, 59.0, 57.9, 57.9, 55.9, 55.9, 55.0, 55.9, 60.1, 63.0, 66.0, 69.1, 72.0, 72.0, 73.9, 73.0, 70.0, 68.0, 64.0, 61.0, 59.0, 61.0, 57.9, 57.9],
[55.9, 55.9, 55.0, 55.0, 54.0, 55.0, 55.0, 55.0, 55.0, 55.0, 55.0, 55.9, 57.0, 57.0, 57.9, 61.0, 64.0, 68.0, 73.0, 75.9, 78.1, 78.1, 77.0, 78.1],
[64.9, 66.0, 64.9, 64.0, 64.0, 63.0, 61.0, 61.0, 61.0, 61.0, 63.0, 66.9, 64.0, 66.0, 64.0, 64.0, 64.0, 62.1, 61.0, 60.1, 59.0, 57.9, 57.9, 57.9],
[57.9, 57.9, 57.9, 57.9, 57.9, 57.9, 57.0, 57.9, 59.0, 60.1, 59.0, 60.1, 60.1, 61.0, 63.0, 64.9, 66.0, 66.0, 66.0, 66.0, 66.9, 66.9, 66.0, 66.0],
[70.0, 70.0, 71.1, 71.1, 71.1, 71.1, 70.0, 70.0, 69.1, 71.1, 71.1, 73.0, 73.9, 75.9, 77.0, 73.9, 73.0, 72.0, 72.0, 72.0, 72.0, 72.0, 72.0, 72.0],
[72.0, 71.1, 71.1, 71.1, 70.0, 68.0, 68.0, 68.0, 66.2, 66.9, 66.0, 66.0, 69.1, 70.0, 70.0, 73.0, 73.9, 75.9, 77.0, 79.0, 78.1, 75.0, 72.0, 69.1],
[66.9, 66.0, 66.0, 66.0, 66.0, 64.9, 64.0, 63.0, 64.0, 66.0, 69.1, 71.1, 73.0, 73.0, 73.0, 73.9, 73.0, 70.0, 61.0, 59.0, 57.0, 55.9, 54.0, 53.1],
[51.1, 51.1, 51.1, 51.1, 51.1, 52.0, 51.1, 51.1, 54.0, 60.1, 64.0, 66.0, 68.0, 69.1, 68.0, 68.0, 66.9, 64.9, 63.0, 60.1, 59.0, 60.1, 57.9, 57.0]
]

print("Average Hour:", average_hour(temps, 0)) # Should be ~61.3
print("Average Day:", average_day(temps, 0)) # Should be ~71.5


I reference
main()
to test it. In the end, it tells me that
TypeError: list indices must be integers or slices, not NoneType


What should be an appropriate placeholder for that then instead of
None
? Also, am I doing the for loop right?

Please let me know. Newbie here (as it is inherently obvious).

Answer

You are indeed using None as an index. Instead, for average_hour, you should iterate over all hours of a fixed day (not None). Similar for average_day:

def average_hour(temps, hour):
    total = 0
    for day in temps:  # each row is a day (list of hours)
        total += day[hour]  # take specified hour from each row (day)
    average = total / len(temps)
    return average

def average_day(temps, day):
    total = 0
    for hour in temps[day]:  # just iterate over the specified row (aka day)
        total += hour
    average = total / len(temps[day])
    return average

> print(average_hour(temps, 0))
> print(average_day(temps, 0))

61.3064516129
71.5041666667