import numbers
def mapped(x):
if isinstance(x,numbers.Number):
return x
for tpe in (int, float):
try:
return tpe(x)
except ValueError:
continue
return 0
for sub in someData:
sub[:] = map(mapped,sub)
print(someData)
[[1.0, 4, 7, -50], [0, 0, 0, 12.5644]]
It will work for different numeric types:
In [4]: from decimal import Decimal
In [5]: someData = [[1.0,4,'7',-50 ,"99", Decimal("1.5")],["foobar",'8 bananas','text','',12.5644]]
In [6]: for sub in someData:
...: sub[:] = map(mapped,sub)
...:
In [7]: someData
Out[7]: [[1.0, 4, 7, -50, 99, Decimal('1.5')], [0, 0, 0, 0, 12.5644]]
if isinstance(x,numbers.Number)
catches subelements that are already floats, ints etc.. if it is not a numeric type we first try casting to int then to float, if none of those are successful we simply return 0
.