字符串上的Numpy'where' [英] Numpy 'where' on string
问题描述
我想在字符串数组上使用numpy.where函数.但是,我这样做没有成功.有人可以帮我解决这个问题吗?
I would like to use the numpy.where function on a string array. However, I am unsuccessful in doing so. Can someone please help me figure this out?
例如,当我在以下示例中使用numpy.where
时,出现错误:
For example, when I use numpy.where
on the following example I get an error:
import numpy as np
A = ['apple', 'orange', 'apple', 'banana']
arr_index = np.where(A == 'apple',1,0)
我得到以下信息:
>>> arr_index
array(0)
>>> print A[arr_index]
>>> apple
但是,我想知道字符串'apple'
匹配的字符串数组A
中的索引.在上面的字符串中,这发生在0和2.但是,np.where
仅返回0,而不返回2.
However, I would like to know the indices in the string array, A
where the string 'apple'
matches. In the above string this happens at 0 and 2. However, the np.where
only returns 0 and not 2.
那么,如何使numpy.where
在字符串上工作?提前致谢.
So, how do I make numpy.where
work on strings? Thanks in advance.
推荐答案
print a[arr_index]
不是array_index
!!
a = np.array(['apple', 'orange', 'apple', 'banana'])
arr_index = np.where(a == 'apple')
print arr_index
print a[arr_index]
这篇关于字符串上的Numpy'where'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!