Reputation: 1243
Say I wanted to find "greater than" in 2 lists.
a = [1,2,3]
b = [0, 0.1, 4]
map( <something>, zip(a,b))
I tried operator module. It has a operator.gt() method. But i couldn't find a way to make use of it with zip. any ideas? Edit: The output is just a True value if any one of the them is True.
thanks
Upvotes: 2
Views: 1678
Reputation: 2309
To just compare the items in lists a
with items in b
, you don't event have to use zip()
:
>>> a = [1, 2, 3]
>>> b = [0, 0.1, 4]
>>> map(operator.gt, a, b)
[True, True, False]
>>>
But on the other hand you've not specified what kind of output you're expecting.
EDIT:
To effectively OR
the result, wrap it with any()
. Like so:
>>> any(map(operator.gt, a,b))
True
Upvotes: 8
Reputation: 879461
In [6]: a = [1,2,3]
In [7]: b = [0, 0.1, 4]
In [8]: [max(a_,b_) for a_,b_ in zip(a,b)]
Out[8]: [1, 2, 4]
or, pithier,
In [9]: map(max, zip(a,b))
Out[9]: [1, 2, 4]
If you want of list of True/False
values (True
where the item in a
is greater than the item in b
, and False
otherwise), you could use itertools.starmap with operator.gt:
In [15]: import itertools as IT
In [17]: list(IT.starmap(operator.gt, zip(a,b)))
Out[17]: [True, True, False]
Upvotes: 2
Reputation: 1690
numpy is ideal for this kind of stuff if you don't mind the additional dependancy.
import numpy as np
a = np.array([1,2,3])
b = np.array([0, 0.1, 4])
c = a > b
Upvotes: 1
Reputation: 3066
a = [1,2,3]
b = [0, 0.1, 4]
map (max, zip(a,b))
Output: [1, 2, 4]
Upvotes: 2