How to iterate over values of one list, change that with a function and add that to a second list?

Viewed 256

I have this list of temperatures:

temp_data =  [19, 21, 21, 21, 23, 23, 23, 21, 19, 21, 19, 21, 23, 27, 27, 28, 30, 30, 32, 32, 32, 32, 34, 34,
         34, 36, 36, 36, 36, 36, 36, 34, 34, 34, 34, 34, 34, 32, 30, 30, 30, 28, 28, 27, 27, 27, 23, 23,
         21, 21, 21, 19, 19, 19, 18, 18, 21, 27, 28, 30, 32, 34, 36, 37, 37, 37, 39, 39, 39, 39, 39, 39,
         41, 41, 41, 41, 41, 39, 39, 37, 37, 36, 36, 34, 34, 32, 30, 30, 28, 27, 27, 25, 23, 23, 21, 21,
         19, 19, 19, 18, 18, 18, 21, 25, 27, 28, 34, 34, 41, 37, 37, 39, 39, 39, 39, 41, 41, 39, 39, 39,
         39, 39, 41, 39, 39, 39, 37, 36, 34, 32, 28, 28, 27, 25, 25, 25, 23, 23, 23, 23, 21, 21, 21, 21,
         19, 21, 19, 21, 21, 19, 21, 27, 28, 32, 36, 36, 37, 39, 39, 39, 39, 39, 41, 41, 41, 41, 41, 41,
         41, 41, 41, 39, 37, 36, 36, 34, 32, 30, 28, 28, 27, 27, 25, 25, 23, 23, 23, 21, 21, 21, 19, 19,
         19, 19, 19, 19, 21, 23, 23, 23, 25, 27, 30, 36, 37, 37, 39, 39, 41, 41, 41, 39, 39, 41, 43, 43,
         43, 43, 43, 43, 43, 43, 43, 39, 37, 37, 37, 36, 36, 36, 36, 34, 32, 32, 32, 32, 30, 30, 28, 28,
         28, 27, 27, 27, 27, 25, 27, 27, 27, 28, 28, 28, 30, 32, 32, 32, 34, 34, 36, 36, 36, 37, 37, 37,
         37, 37, 37, 37, 37, 37, 36, 34, 30, 30, 27, 27, 25, 25, 23, 21, 21, 21, 21, 19, 19, 19, 19, 19,
         18, 18, 18, 18, 18, 19, 23, 27, 30, 32, 32, 32, 32, 32, 32, 34, 34, 34, 34, 34, 36, 36, 36, 36,
         36, 32, 32, 32, 32, 32, 32, 32, 32, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 28, 28]

I have imported a module that I created with two functions, to change the temperature from Fahrenheit to Celsius and the other to classify into 4 classes according to Celsius.

from temp_functions import fahr_to_celsius, temp_classifier

So, I also created an empty list where the classified according to Celsius will go:

temp_classes =[]

and a for loop after that:

the for loop should iterate over all values in temp_data, change them via

fahr_to_celsius function, and than append them to the empty list temp_celsius.

for t in temp_data:
    temp_celsius = []
    temp_celsius.append(fahr_to_celsius(t))

Issue: I just get the first value. I tried range, len, =+1 and several other but no luck.

EDIT: Adding info from OP comment:

This is an assignment that I am working on:

Iterate over the Fahrenheit temperature values in the temp_data list (one by one) and inside the loop: Create a new variable called temp_celsius in which you should assign the temperature in Celsius using the fahr_to_celsius function to convert the Fahrenheit temperature into Celsius. Create a new variable called temp_class in which you should assign the temperature class number (0, 1, 2, or 3) using the temp_classifier function Add the temp_class value to the temp_classes list

4 Answers

Another strategy would be to use a Python list comprehension:

temp_celsius = [fahr_to_celsius(t) for t in temp_data]

You are creating a new list at each iteration. move the creation of the list outside of the for loop :

temp_celsius = []
for t in temp_data:
    temp_celsius.append(fahr_to_celsius(t))

You could also do this using map although some folks don't consider this to be "pythonic"

map() maps the function to each item in an iterable and takes the form of

map(function, iterable) #where an iterable is a list, set, tuple

It is essentially the same as the list comprehension.

python3:

temp_celcius = list(map(fahr_to_celsius, temp_data)) # explicitly call it a list

python2:

temp_celcius = map(fahr_to_celsius, temp_data) # interpreted as a list

You might consider to use pandas to have a table view

import pandas as pd
df = pd.DataFrame({"fahr":temp_data})

df["celsius"] = df["fahr"].apply(fahr_to_celsius)
# or
df["celsius"] = fahr_to_celsius(df["fahr"])
# or (even faster)
df["celsius"] = fahr_to_celsius(df["fahr"].values)

Related