首页 > 解决方案 > 元素大于三个或更多列表

问题描述

基于以下列表

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

我想做一个元素大于比较,使得结果数组中的真值只为真,如果第一个数组中的相应元素大于第二个数组中的元素并且第二个数组中的元素大于第三个数组中的元素。

IE

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]
...

numpynp.greater似乎只适用于两个列表

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

但不是3:

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

那么,假设列表长度相等,我该如何进行比较呢?

标签: pythonnumpy

解决方案


尝试numpy.logical_and

numpy.logical_and(np.greater(l1,l2), np.greater(l2,l3))

推荐阅读