35

I have two lists a and b:

a  =   [3,    6,   8,   65,   3]
b  =   [34,   2,   5,   3,    5]

c gets [3/34, 6/2, 8/5, 65/3, 3/5]

Is it possible to obtain their ratio in Python, like in variable c above?

I tried a/b and got the error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'list' and 'list'
martineau
  • 119,623
  • 25
  • 170
  • 301
ely
  • 495
  • 2
  • 9
  • 12

6 Answers6

44
>>> from __future__ import division # floating point division in Py2x
>>> a=[3,6,8,65,3]
>>> b=[34,2,5,3,5]
>>> [x/y for x, y in zip(a, b)]
[0.08823529411764706, 3.0, 1.6, 21.666666666666668, 0.6]

Or in numpy you can do a/b

>>> import numpy as np
>>> a=np.array([3,6,8,65,3], dtype=np.float)
>>> b=np.array([34,2,5,3,5], dtype=np.float)
>>> a/b
array([  0.08823529,   3.        ,   1.6       ,  21.66666667,   0.6       ])
jamylak
  • 128,818
  • 30
  • 231
  • 230
  • Actually it depends on what the OP meant with "dividing lists". Suppose he wanted to interpret it in the sense of set fraction (e.g. elements which appear in both lists). – Stefano Sanfilippo May 07 '13 at 12:01
  • Guessing is for comments, not for answers. Actually, the first thing I thought was `A\B` set fraction. – Stefano Sanfilippo May 07 '13 at 12:04
  • 3
    @esseks actually it's fine to post answers of what you think they question means if you are pretty sure you are correct and if OP says it isn't what they want I will delete my answer – jamylak May 07 '13 at 12:06
  • 1
    @ esseks With a/b I meant the division of the first element in a by the first element in b and so on. The answer received is essentially what I was looking for. @ jamylak Thanks for both the answers, it is useful to know different ways to get the same result. – ely May 07 '13 at 12:25
21

The built-in map() function makes short work of these kinds of problems:

>>> from operator import truediv
>>> a=[3,6,8,65,3]
>>> b=[34,2,5,3,5]
>>> map(truediv, a, b)
[0.08823529411764706, 3.0, 1.6, 21.666666666666668, 0.6]
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • Thank you for this answer ! when doing this from sql queries transformed to lists in numpy I get `argument 2 to map() must support iteration` do you have any idea on how to solve this error ? – Revolucion for Monica May 22 '17 at 16:40
13

You can do this using list comprehension (element by element):

div = [ai/bi for ai,bi in zip(a,b)]

Note that if you want float division, you need to specify this (or make the original values floats):

fdiv = [float(ai)/bi for ai,bi in zip(a,b)]
sapi
  • 9,944
  • 8
  • 41
  • 71
  • 3
    Or do `from __future__ import division`, or use Python 3 (as the OP may well be already). – lvc May 07 '13 at 12:02
  • 1
    @lvc - I don't like making an import that can completely change the meaning of code unless absolutely necessary. A significant amount of the code I write would break spectacularly if I did `from __future__ import division`. – sapi May 07 '13 at 13:16
9

Use zip and a list comprehension:

>>> a = [3,6,8,65,3]
>>> b = [34,2,5,3,5]
>>> [(x*1.0)/y for x, y in zip(a, b)]
[0.08823529411764706, 3.0, 1.6, 21.666666666666668, 0.6]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

Using numpy.divide

c=np.divide(a,b)
whiletrue
  • 10,500
  • 6
  • 27
  • 47
0

You can use the following code:

a  =   [3,    6,   8,   65,   3]
b  =   [34,   2,   5,   3,    5]

c = [float(x)/y for x,y in zip(a,b)]
print(c)