I have:
s= 'Lot Size: 1.52 acres'
I want to return a float number only (1.52)
I tried:
>>> o =[s for s in str.split('') if s.isdigit() if s=='.']
>>>
>>>o
>>>[]
How can I get this working?
This will assign to o a list of the "words" in msg that can be interpreted as floats:
def isFloat(n):
try:
return float(n)
except:
return None
o = list(filter(isFloat,msg.split()))
You could try the following, with a singular condition and in keeping with your original format (using python 3.6.8) :)
Multiple condition syntax:
[ x for x in x.do() if 'x' in x OR/AND if x == 1]
Example:
s = 'Lot Size: 1.52 acres'
o = [s for s in s.split(' ') if '.' in s]
print(o[0])
Output:
1.52
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With