python - 两个numpy ndarrays的字典序比较

标签 python arrays numpy

我找不到以词典编排方式比较两个(在我的例子中是多维)数组的直接方法。

即。

a = [1,2,3,4]
b = [4,0,1,6]

对于 a < b我想得到 true我在哪里得到 [true, false, false, true]
对于 a > b我想得到 false我在哪里得到 [false, true, true, false]

最佳答案

如果问题只是关于寻找是否a<>b , 那么下面的应该工作。

def fn(a, b):
    # finds index of the first non matching element
    idx = np.where( (a>b) != (a<b) )[0][0]

    if a[idx] < b[idx]: print "a < b" 
    if a[idx] > b[idx]: print "a > b" 

关于python - 两个numpy ndarrays的字典序比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38044731/

相关文章:

c - 如何遍历数组以将它们复制到另一个 uint32_t 类型的数组中?

c - 在C语言中,如何将数组传递给函数?

python - 如何将 % 添加到 numpy 数组中的每个值?

python - 在循环外添加矩阵

python - Pandas 中的矢量化操作

python - 在给定高度和值的情况下递归生成三角形格式的列表列表

python - 从 Python 中的趋势线图中提取 y 值

javascript - 如何在javascript中转换数据格式

python - 从 Django-Oscar 扩展客户的 View

python - 运行本地服务器时 django 中的数据库错误?