If you're doing a lot of arithmetic on arrays, you may want to consider using numpy
. On top of being easier to use, and usually much faster, it's also more flexible.
For example:
>>> divisors = np.array([1,2,3,4,5,6,3,31,1,0,120,0,0])
>>> fractions = 10. / divisors
>>> fractions
array([ 10. , 5. , 3.33333333, 2.5 ,
2. , 1.66666667, 3.33333333, 0.32258065,
10. , inf, 0.08333333, inf, inf])
Compare to:
>>> fractions = []
>>> for divisor in divisors:
... if divisor == 0:
... fractions.append(float('inf'))
... else:
... fractions.append(10. / divisor)
Or even:
>>> fractions = [10. / divisor if divisor else float('inf')
... for divisor in divisors]
numpy
isn't always the answer, but it's worth taking a look at.