I have a list of values in a list p
, which is as follows:
p = ['38.5 C', '51 %', '40.5 W/m2', '60 ohm']
I wish to extract the numerical values in the list, into a list q
, which will have values as follows:
q = [38.5, 51, 40.5, 60]
How do I achieve this in Python 3?
My attempt:
import re
a = '39.5 C'
b = re.findall(r'd+.*d*', a)
b = ['39.5']
Now I try:
p = ['38.5 C', '51 %', '40.5 W/m2', '60 ohm']
b = re.findall(r'd+.*d*', p)
That's not working! Therefore
l = []
for s in p:
b = re.findall(r'd+.*d*', s) #returns a list of numbers
l.append(float(b[0])) #because the earlier command returns a list of numbers.
I want this in a more elegant way!
Aucun commentaire:
Enregistrer un commentaire