字符串上的Numpy'where' [英] Numpy 'where' on string

查看:61
本文介绍了字符串上的Numpy'where'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在字符串数组上使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆