1

I have two lists of temperatures with Fahrenheit and Celsius, e.g.:(['23,6C', '16,5C', '38,4C',...]and ['19.6F', '72.3F', '81.75F', '18.02F', ) the data type is string and I want to convert them into floats to be able to compute them into Kelvin. The letters can be removed.

I already tried to remove the letters with a for loop, but then I became a list of strings of each value before and after the point or the comma. When I want to convert them directly, it does not work because of the letters after the values.

for pos in list_cel:
    for buchst in pos:
        if buchst == "C":
            buchst.replace("C", " ")
        else:
            nlist_cel.append(buchst)
print(nlist_cel)

#gives me a list of strings, seperated after each comma

like ['23','6',...] instead of [23,6 or 23.6]

The output should look like this

[23.6, 16.5, 38.4,] 
[19.6, 72.3, 81.75,]
Al Imran
  • 882
  • 7
  • 29
besch150
  • 73
  • 1
  • 10
  • Possible duplicate of [How do you convert a list of strings to a list of floats using Python?](https://stackoverflow.com/questions/51844300/how-do-you-convert-a-list-of-strings-to-a-list-of-floats-using-python) – MisterMiyagi May 21 '19 at 11:54
  • 1
    Possible duplicate of [Convert all strings in a list to int](https://stackoverflow.com/questions/7368789/convert-all-strings-in-a-list-to-int) – Andras Deak -- Слава Україні May 21 '19 at 11:55
  • I already read these questions but my problem was different so that did not help me. I would not have posted a question if there has been a possibility to solve it by reading other questions and answers. I always try to solve my problem with reading, which turns out well in 95% of the cases. – besch150 May 21 '19 at 12:09

7 Answers7

1

Using list comprehension, more details

str.replace(old, new) - Return a copy of the string with all occurrences of substring old replaced by new

fahrenheit =['19.6F', '72.3F', '81.75F', '18.02F']

celsius = ['23,6C', '16,5C', '38,4C']

fahrenheit = [float(i.replace("F","")) for i in fahrenheit ]
celsius = [float(i.replace("C","").replace(",",".")) for i in celsius ]

print(fahrenheit)
print(celsius)

O/P:

[19.6, 72.3, 81.75, 18.02]
[23.6, 16.5, 38.4]
bharatk
  • 4,202
  • 5
  • 16
  • 30
  • Thank you very much for this solution. It helped me, because I am not very good in list comprehension myself. – besch150 May 21 '19 at 12:04
0

convert it on float

for pos in list_cel:
for buchst in pos:
    if "C" in buchst:
       val = buchst.replace("C", " ")
       if ',' in val:
          nlist_cel.append( float (val.replace(",", ".")))
       else:
          nlist_cel.append(float(val))

 print(nlist_cel)
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

Replacing comma to dot and sliced the unit

lst=['23,6C', '16,5C', '38,4C',]
lst=[float(i[:-1].replace(',','.')) for i in lst]

Output

[23.6, 16.5, 38.4]

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
0

Using a list comprehension with str.replace and str.strip

Ex:

data = (['23,6C', '16,5C', '38,4C'], ['19.6F', '72.3F', '81.75F', '18.02F'])
print([[float(j.replace(",", ".").rstrip("CF")) for j in i] for i in data])

Output:

[[23.6, 16.5, 38.4], [19.6, 72.3, 81.75, 18.02]]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Try this -

for value in raw_list_value:
    if ',' in value:
        new_val = float('.'.join(i[:-1].split(','))
    else:
        new_val = float(value[:-1])
    temp_list.append(new_value)
0
list_cel = ['23,6C', '16,5C', '38,4C']
list_far = ['19.6F', '72.3F', '81.75F', '18.02F']


list_cel_float = map(lambda x:float(x.replace(',','.').replace('C','')), list_cel)
list_far_float = map(lambda x:float(x.replace(',','.').replace('F','')), list_far)

print list_cel_float
print list_far_float
framontb
  • 1,817
  • 1
  • 15
  • 33
0

A quick solution if you know that all the temps have a 'F' or 'C' appended:

fah = ['19.6F', '72.3F', '81.75F', '18.02F']
cel = ['23,6C', '16,5C', '38,4C']

If just checks the first element in the list to see if there is an 'F' or 'C' and proceeds accordingly to return the list. Gets rid of the commas too, it would be a lot easier to not have to deal with them later.

def f_or_c_temp(temp_list):
    if 'F' in temp_list[0]:
        return [temp.replace('F','') for temp in f]
    if 'C' in temp_list[0]:
        c = [temp.replace('C','') for temp in temp_list]
        return [temp.replace(',','.') for temp in c]

So for Fahrenheit

f_or_c_temps(fah)

Output:

['19.6', '72.3', '81.75', '18.02']

And for Celcius

f_or_c_temps(cel)

Output:

['23.6', '16.5', '38.4']
Coffee and Code
  • 885
  • 14
  • 16