假设我有一个 numpy 数组:
arr = np.array([1,2,3,4,4,5,3,2,10])
和一个索引数组:
索引 = np.array([0,1,4,6])
我可以编写一个简单的函数来完成这项工作,但我想知道 numpy 是否有一个像 np.get(arr,indexs)
这样的内置函数,在本例中返回,np.array([1,2,4,3]
.
最佳答案
这称为Advanced Indexing :
triggered when the selection object is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.
Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view).
您的情况是整数高级索引,您传递要检索的索引数组。正如 sascha 在评论中指出的那样,这将创建数据的副本,因此新数组将独立于原始数组而存在(即写入它不会修改原始数组)。
关于python - 如何获取 numpy 数组中不连续索引的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48345305/