I've been trying all day to have the max/min values inside of a csv file. I try to put them in an array and find the max/min value inside said array but I either get TypeError: '<' not supported between instances of 'int' and 'list' or 'float object not iterable'
. If I could get some pointers on where to start correcting this code, I would appreciate it.
def get_readings_from_file(filename: str, skip_first_line: bool) -> list[WeatherReading]:
with open(filename, "r") as file:
all_readings: list[WeatherReading] = []
maxHum, maxTemp, minPress = [], [], []
first_line = True
for line in file:
if skip_first_line and first_line:
first_line = False
continue
def max_temperature(a):
return max(a)
def max_humidity(b):
return max(b)
def min_pressure(c):
return min(c)
split = line.split(",")
temperature = float(split[0])
maxTemp.append(temperature)
max_temperature(maxTemp)
pressure = float(split[1])
minPress.append(pressure)
min_pressure(minPress)
humidity = float(split[2])
maxHum.append(humidity)
max_humidity(maxHum)
if valid_reading(maxTemp, minPress, maxHum):
reading = WeatherReading(maxTemp, minPress, maxHum)
all_readings.append(reading)
return all_readings
CodePudding user response:
You have this:
if valid_reading(maxTemp, minPress, maxHum):
Passing maxTemp
etc. to valid_reading
. valid_reading
expects floating point numbers, but the arguments you're passing are in fact lists of these values, and thus you get the error if you run get_readings_from_file
.
CodePudding user response:
It is because you forget to change your lists to their min/max:
maxTemp = max_temperature(maxTemp) # instead of simply 'max_temperature(maxTemp)'
minPress = min_pressure(minPress)
maxHum = max_humidity(maxHum)
If I can give an advice: you should avoid changing the type of your variables and you can use mypy
module to help you catching this kind of error.