The pythonic answer is to wrap it in a function, e.g.:
def ratio(a, b):
if b == 0:
return 0
else:
return a / b
feature_map.append(ratio(numOfHe, numOfShe))
The exact form of the function depends on the rest of you code, but if you're writing a line like that hundreds of times, then you should probably be wrapping it in a function, or at least using a loop. Also, variable names like numOfHe
and numOfShe
hint that you might be better served by a dict.
UPDATE
I see from your code link that each calc is actually totally different, so you probably can't easily loop it. Since the calcs are still relatively simple, you could try a trick with eval
like this:
calcs = [
...
(12, 'h + ha + hw + hy'),
(13, '(h + ha) / (hw + hy)'),
...
]
for index, calc in calcs:
try:
v = eval(calc, locals())
except ZeroDivisionError:
v = 0
feature_map.append(v)
You could also add other info to calcs
, and use a namedtuple instead. You could also do use classes instead to evaluate the calcs dynamically as you need them, if that helps.