Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用NumPy将一个数组与另一个数组建立索引_Python_Arrays_Numpy - Fatal编程技术网

Python 使用NumPy将一个数组与另一个数组建立索引

Python 使用NumPy将一个数组与另一个数组建立索引,python,arrays,numpy,Python,Arrays,Numpy,给出一些数据 In [1]: import numpy as np In [2]: x = np.array(['a', 'b', 'b', 'a']) 和一个排序索引 In [3]: i = np.array(['a', 'b']) 我想找到索引中每个数据项的位置 In [4]: # solution here array([0, 1, 1, 0]) 这有点像分类法。我不想在这里使用熊猫。我想在固定长度的字符串上执行此操作。我需要这一点来提高效率。您可以使用: 该函数找出x的每个元素应放

给出一些数据

In [1]: import numpy as np
In [2]: x = np.array(['a', 'b', 'b', 'a'])
和一个排序索引

In [3]: i = np.array(['a', 'b'])
我想找到索引中每个数据项的位置

In [4]: # solution here
array([0, 1, 1, 0])
这有点像分类法。我不想在这里使用熊猫。我想在固定长度的字符串上执行此操作。我需要这一点来提高效率。

您可以使用:

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

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