使用NumPy对一个数组和另一个数组进行索引

时间:2022-09-22 12:33:36

Given some data

给一些数据

In [1]: import numpy as np
In [2]: x = np.array(['a', 'b', 'b', 'a'])

And a sorted index

和一个分类指数

In [3]: i = np.array(['a', 'b'])

I want to find the location of each data entry within the index

我想找到索引中每个数据条目的位置

In [4]: # solution here
array([0, 1, 1, 0])

This is a bit like categoricals. I don't want to use Pandas here. I want to do this on fixed length strings. I need this to be somewhat efficient.

这有点像分类。我不想在这里用熊猫。我想在固定长度的弦上做这个。我需要这个有点效率。

1 个解决方案

#1


5  

You could use np.searchsorted:

您可以使用np.searchsorted:

>>> np.searchsorted(i, x)
array([0, 1, 1, 0])

The function finds out the index at which each element of x should be placed in i in order to maintain sorted order.

函数找出x的每个元素应该放在i中的索引,以便保持排序的顺序。

#1


5  

You could use np.searchsorted:

您可以使用np.searchsorted:

>>> np.searchsorted(i, x)
array([0, 1, 1, 0])

The function finds out the index at which each element of x should be placed in i in order to maintain sorted order.

函数找出x的每个元素应该放在i中的索引,以便保持排序的顺序。