2

I have a mix of strings and floats in the list:

line = [u'6.0', u'5.0', u'1.2', u'S\xf3lo', u'_START_', u'utilizamos', u'only', u'We', u'use', u'0', u'0', u'1', u'0', u'0', u'0', u'0.2', u'0.1', u'0.2', u'0', u'0', u'0', u'ADV', u'RB', u'9', u'0', u'OK']

I want to convert the "cast-able" items in the list to float, i.e. the output:

[6.0, 5.0, 1.2, u'S\xf3lo', u'_START_', u'utilizamos', u'only', u'We', u'use', 0, 0, 1, 0, 0, 0, 0.2, 0.1, 0.2, 0, 0, 0, u'ADV', u'RB', 9, 0, u'OK']

I've tried this but is there a less verbose way to do it?

def super_munger(_lst):
  lst = []
  for item in _lst:
    try:
      item = float(item)
    except:
      item = item
    lst.append(item)
  return item
alvas
  • 115,346
  • 109
  • 446
  • 738

2 Answers2

3

You can use something like this -

def float_cast(item):
    try:
        return float(item)
    except ValueError:
        return item

result_lst = [float_cast(item) for item in lst] #`lst` is your original list

If you want it in a function, you can do -

def super_munger(_lst):
    return [float_cast(item) for item in _lst]

Demo -

>>> def float_cast(item):
...     try:
...         return float(item)
...     except ValueError:
...         return item
...
>>> line = [u'6.0', u'5.0', u'1.2', u'S\xf3lo', u'_START_', u'utilizamos', u'only', u'We', u'use', u'0', u'0', u'1', u'0', u'0', u'0', u'0.2', u'0.1', u'0.2', u'0', u'0', u'0', u'ADV', u'RB', u'9', u'0', u'OK']
>>> result = [float_cast(item) for item in line]
>>> result
[6.0, 5.0, 1.2, 'Sólo', '_START_', 'utilizamos', 'only', 'We', 'use', 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.2, 0.1, 0.2, 0.0, 0.0, 0.0, 'ADV', 'RB', 9.0, 0.0, 'OK']
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

Use regular expression to check that string can be converted to float:

import re

line = [float(x) if re.match("^\d+?\.\d+?$", x) else x for x in line]
Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43