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