+4 votes
in Programming Languages by (18.8k points)
I want to divide one list by another list element-wise. It should return element-wise quotient and remainder simultaneously. What function should I use?

E.g.

a= [12, 13, 14, 15]

b= [2, 3, 4, 5]

Result should be: [(6, 0), (4, 1), (3, 2), (3, 0)]

1 Answer

0 votes
by (33.0k points)

The divmod() function of Numpy returns the element-wise quotient and remainder simultaneously. You can then use the zip() function to get the result in your desired format.

Here is an example:

>>> import numpy as np
>>> x=[12,13,14,15]
>>> y=[2,3,4,5]
>>> q,r=np.divmod(x,y)
>>> q
array([6, 4, 3, 3])
>>> r
array([0, 1, 2, 0])
>>> list(zip(q,r))
[(6, 0), (4, 1), (3, 2), (3, 0)]

...