Python 比较三个数组 return 如果值是三个数组中最大的则为 True
Python comparing three arrays and return the True if the value is the largest among the three
假设我有三个数组
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() 来计算每个变量的数量是最大的。
谢谢
通常以下代码应该有效:
a, b, c = [x==np.max(x) for x in (a, b, c)]
假设我有三个数组
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() 来计算每个变量的数量是最大的。 谢谢
通常以下代码应该有效:
a, b, c = [x==np.max(x) for x in (a, b, c)]