这里发生的是Python
tuples are compared by position.
所以当你这样做的时候
my_array == template
您实际正在做的(按行)是:
('Apple', 'Orange', 5.0) == 'Apple'
('Apple', 'Orange', 5.0) == 'Orange'
('Apple', 'Orange', 5.0) == 5.0
要验证情况是否如此,请尝试使用以下示例进行实验:
>>> other_array = np.array(['Apple', 'Orange', 5.0] * 3).reshape(3,3)
>>> other_array
array([['Apple', 'Orange', '5.0'],
['Apple', 'Orange', '5.0'],
['Apple', 'Orange', '5.0']], dtype='<U6')
>>> other_array == template
array([[ True, True, True],
[ True, True, True],
[ True, True, True]])
我不知道有什么非黑客的方法可以解决这个问题,并让直接的平等比较发挥作用。如果黑客攻击已经足够,并且您的阵列不是太大,您可以尝试:
mask = np.array(list(map(lambda x: x == template,
my_array.flatten()))).reshape(my_array.shape)
或
mask = np.array([x == template for x in my_array.flatten()]).reshape(my_array.shape)
是否有需要元组数组的原因?数组中不能有另一个维度,或者可以使用pandas作为分类变量吗?