0

I have a list of strings. Some of these strings can be converted to floats, other not. I'm trying to extract those that can be converted to floats. For now, I am using the following code

my_strings = ['1', '42', '.3', 'a', 'b', 'c']
my_floats = []
for e in my_strings:
    try:
        my_floats.append(float(e))
    except ValueError:
        pass

Is there a way of doing this operation as a list comprehension?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
usernumber
  • 1,958
  • 1
  • 21
  • 58
  • Check if string has only 1 `.` and the others are digits. It may have a `-`. If all these are met, we may be able to convert to float – Joe Ferndz Jan 29 '21 at 17:06
  • Also need to consider the 'e' in the string – Joe Ferndz Jan 29 '21 at 17:15
  • 4
    Why do you want to convert it into a list comp? What you have now is fine. – wjandrea Jan 29 '21 at 17:15
  • 2
    The absence of a nice string predicate for testing in one expression if something is convertible to a float precludes a readable list comprehension. I agree with @wjandrea here. List comprehensions are nice, but not a universal tool for list creation. – John Coleman Jan 29 '21 at 17:17
  • @JohnColeman well, one could trivially wrap the exception handling in a function, and then use that, but I agree, this for-loop is perfectly fine as it is, and people shouldn't be striving to turn everything into a list comprehension. – juanpa.arrivillaga Jan 29 '21 at 17:35

2 Answers2

0

You could write a safeFloat function that converts a string to a float and uses a default value for invalid strings. This could be used in a list comprehension or mapped with a filter if you don't want items that aren't numeric:

def safeFloat(S,default=None):
    try:    return float(S)
    except: return default


my_strings = ['1', '42', '.3', 'a', 'b', 'c']

my_floats  = [safeFloat(s,0) for s in my_strings]

[1.0, 42.0, 0.3, 0, 0, 0]

my_floats = [float(s) for s in my_strings if safeFloat(s) != None]

[1.0, 42.0, 0.3]

my_floats  = list(filter(None,map(safeFloat,my_strings)))

[1.0, 42.0, 0.3]

If you're doing this often on lists or iterators, you can further encapsulate the conversion in a function that converts and filters multiple items

def getFloats(S,default=None):
    yield from filter(None,map(safeFloat,S))

my_floats = [*getFloats(my_strings)]

[1.0, 42.0, 0.3]
Alain T.
  • 40,517
  • 4
  • 31
  • 51
-3

You should not rely on exceptions for your code logic. Try this:

my_strings = ['1', '42', '.3', 'a', 'b', 'c']
my_floats = [float(string) for string in my_strings if string.replace('.','',1).isdigit()]
Tom Gebel
  • 744
  • 1
  • 4
  • 13
  • 3
    What about negative float literals or those of the form `'1e7'`? – John Coleman Jan 29 '21 at 17:11
  • 1
    If the string is an ip address, we should eliminate that – Joe Ferndz Jan 29 '21 at 17:15
  • 3
    Why shouldn't you rely on exceptions? – Axe319 Jan 29 '21 at 17:16
  • @JohnColeman Of course these are special cases one could additionally implement, but for the given example by OP this works. Not a reason to downvote. – Tom Gebel Jan 29 '21 at 17:20
  • 4
    [Relying on exceptions for code logic is fine](https://stackoverflow.com/a/16138864/4518341). As well, this won't work for the "exotic" floats, `float('nan')`, `float('inf')`, and `float('-inf')`, in addition to the normal ones John already mentioned. – wjandrea Jan 29 '21 at 17:20
  • @TomGebel Code that is aimed for a specific example rather than a more general specification tends to be fragile. In any event -- I didn't downvote you (though the existence of my comment may have partially inspired others). – John Coleman Jan 29 '21 at 17:22
  • 2
    Using exception handling is the idiomatic way to check if a string is convertible to a `float`. – juanpa.arrivillaga Jan 29 '21 at 17:36