fork download
  1. def try_float(x):
  2. try:
  3. float(x.strip())
  4. return True
  5. except (TypeError, ValueError):
  6. return False
  7.  
  8. value = '1, 2.3, 4, 5, blah, 6.78'
  9. numbers = [float(x) for x in value.split(',') if try_float(x)]
  10. print(numbers)
Success #stdin #stdout 0.01s 7896KB
stdin
Standard input is empty
stdout
[1.0, 2.3, 4.0, 5.0, 6.78]