+2 votes
in Programming Languages by (18.8k points)
I want to do element-wise division without using the "for" loop. What function should I use?

E.g.

a= [12,13,14,15]

b= [2,3,4,5]

a/b = [6, 4.33333333, 3.5, 3]

1 Answer

0 votes
by (33.0k points)

You can use Numpy's divide() function to do an element-wise division of a list by another list.

For example:

>>> a=np.array([12,13,14,15])
>>> b=np.array([2,3,4,5])
>>> import numpy as np
>>> a=np.array([12,13,14,15])
>>> b=np.array([2,3,4,5])
>>> np.divide(a,b)
array([6.        , 4.33333333, 3.5       , 3.        ])

...