Elementwise operations over tuples in Python
python, python-3.x
Solution
map function
>>> a = (1, 2, 4)
>>> b = (1.1, 2.1, 4.1)
>>> map(lambda a,b: 100*abs(a-b)/a < 3, a, b)
[False, False, True]
EDIT
of course instead of map, you can use list comprehensions, like BrenBarn did http://docs.python.org/tutorial/datastructures.html#nested-list-comprehensions
EDIT 2 zip removed, thanks for DSM to point it out that zip is not needed
Problem
Are there any built-in functions that allow elementwise operations over tuples in Python 3? If not, what is the "pythonic" way to perform these operations? Example: I want to take the percent difference between `a` and `b` and compare them to some threshold `th`. ``` >>> a = (1, 2, 4) >>> b = (1.1, 2.1, 4.1) >>> # compute pd = 100*abs(a-b)/a = (10.0, 5.0, 2.5) >>> th = 3 >>> # test threshold: pd < th => (False, False, True) ```