首页 > 解决方案 > 比较两个不同大小的数组,按元素?

问题描述

假设我有两个不同大小的数组:

In [79]: tr
Out[79]: array([1, 1, 0, 6, 0, 3])

In [80]: br
Out[80]: array([ 9, 26, 24, 18, 14, 12,  8])

我想确保 br 的所有元素都大于 tr 的所有元素,即 br > tr

 ValueError: operands could not be broadcast together with shapes (6,) (7,) 

标签: pythonarraysnumpycompare

解决方案


您可以简单地检查 br 中的最小数字是否大于 tr 中的最大数字:

if min(br) > max(tr):
    # all the element in br are bigger 
else:
    # there is at least one value in tr bigger or equal than one value in br

推荐阅读