Elementwise greater-than with three or more lists

user1934212 :

Based on the following lists

l1 = [1,2,3]
l2 = [2,3,4]
l3 = [3,4,2]

I want to do an elementwise greater than comparison such that the truth value in the resulting array is only true, iff the corresponding element in the 1st array is greater than the element in the 2nd array and the element in the 2nd array is greater than the element in the 3rd arry.

i.e.

print(elementwise_greather_than(l3,l2,l1))
[True,True,False]
print(elementwise_greather_than(l2,l1,l3))
[False,False,True]
print(elementwise_greather_than(l1,l3,l2))
[False,False,False]
...

numpy's np.greater only seems to work with two lists

import numpy as np
print(np.greater(l1,l3))
[False False  True]

but not with 3:

print(np.greater(l1,l3,l2))
TypeError: return arrays must be of ArrayType

So how can I do this comparison, assuming lists of equal lengths?

Al Wld :

Assuming lists of equal length

def elementwise_greather_than(a,b,c):
    return [x > y > z for x,y,z in zip(a,b,c)]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=4007&siteId=1