0

Python: How to divide list with another list?

Hi, a = [1, 2, 3, 4, 5] b = [10, 20, 30, 40, 50] z = b/a It seems like list cannot divide by another list. How do you divide b[0] by a[0]

3rd Feb 2019, 1:37 PM
Terry
Terry - avatar
6 RĂ©ponses
+ 9
a = [1, 2, 3, 4, 5] b = [10, 20, 30, 40, 50] z = [m/n for m, n in zip(b, a)] print(z) # [10.0, 10.0, 10.0, 10.0, 10.0]
3rd Feb 2019, 2:05 PM
Anna
Anna - avatar
+ 8
import numpy as np a = np.array([10, 20, 30, 40, 50]) b = np.arange(1, 6) print(a / b) print(a / 10)
3rd Feb 2019, 2:38 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 6
print(*map(lambda x, y: x/y, b, a))
3rd Feb 2019, 2:10 PM
HonFu
HonFu - avatar
+ 5
a = [1,2,3,4,5] b = [10,20,30,40,50] c = [value/a[index] for index,value in enumerate(b)] print(c)
3rd Feb 2019, 1:54 PM
code learner
code learner - avatar
+ 5
Wow so many solutions, thank you all
3rd Feb 2019, 11:22 PM
Terry
Terry - avatar
+ 4
z = map(lambda n1, n2: n1 / n2, b, a)
3rd Feb 2019, 2:53 PM
giannismach
giannismach - avatar