+2 votes
in Programming Languages by (74.2k 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

+1 vote
by (349k points)
selected by
 
Best answer

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.        ])


...