【问题标题】:Python comparing three arrays and return the True if the value is the largest among the threePython比较三个数组,如果值是三个数组中最大的,则返回True
【发布时间】:2021-07-24 08:06:17
【问题描述】:

假设我有三个数组

a = np.array([1,3,5])
b = np.array([2,4,3])
c = np.array([3,2,1])

我想计算每个变量是最大变量的次数。 这是我想要的结果:

a = [False, False, True] #since the last element of a, 5 is the largest among the three arrays
b = [False, True, False] #since the second element of b, 4 is the largest among the three arrays
c = [True, False, false] #since the first element of c, 3 is the largest among the three arrays

这样我就可以使用 sum() 来计算每个变量的最大数量。 谢谢

【问题讨论】:

    标签: python arrays numpy numpy-ndarray


    【解决方案1】:

    通常以下代码应该可以工作:

    a, b, c = [x==np.max(x) for x in (a, b, c)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2022-01-11
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多