Chr*_*rei 0 python numpy python-2.7
如何确定以某个字符串开头的 numpy 数组中元素的索引(例如 using startswith)?
例子
大批:
test1234
testworld
hello
mynewcar
test5678
Run Code Online (Sandbox Code Playgroud)
现在我需要值以test. 我想要的结果是:
[0,1,4]
Run Code Online (Sandbox Code Playgroud)
您可以使用np.char.startswith来获取匹配的掩码,然后np.flatnonzero获取匹配的索引 -
np.flatnonzero(np.char.startswith(a, 'test'))
Run Code Online (Sandbox Code Playgroud)
样品运行 -
In [61]: a = np.array(['test1234', 'testworld','hello','mynewcar','test5678'])
In [62]: np.char.startswith(a, 'test')
Out[62]: array([ True, True, False, False, True], dtype=bool)
In [63]: np.flatnonzero(np.char.startswith(a, 'test'))
Out[63]: array([0, 1, 4])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1387 次 |
| 最近记录: |