Sara-Ann Piscopo Sara-Ann Piscopo - 7 months ago 30
Python Question

Append float to list with for loop in python

I am trying to add a list of monthly temperatures into a big list that will contain 24 months of temperatures. The problem is that they are given in floats, but to append items, they must be integers.

temperatures = []
np.array(temperatures, dtype = np.float32)

(after my first month, I append my values to the big list temperatures and empty TEMP1 for the next month)

for item in TEMP1:
np.insert(temperatures, TEMP1[item])

the message of error is :

File "/home/piscopo/Bureau/EC/", line 87, in <module>
np.insert(temperatures, TEMP1[item])

TypeError: list indices must be integers, not numpy.float32

Thank you


You have to save your nparray in a variable and then you can add your TEMP1 monthly temperatures all at once with the method append() like this :

import numpy as np

TEMP1 = [22.4, 14.4, 12.3]
temperatures = []
floatTemperatures = np.array(temperatures, dtype = np.float32)

floatTemperatures = np.append(floatTemperatures, TEMP1)