python - 如何获取 numpy 数组中不连续索引的元素?

标签 python numpy

假设我有一个 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/

相关文章:

python - 如何 reshape 多个时间序列信号以与 sns.tsplot 一起使用?

python - 处理移动平均线中的零

python - 正则表达式替换无法在Python中用正则表达式变量替换

python - RawConfigParser 未写入空字符串

python - 将 numpy float 组转换为整数,替换 NaN 值

python - 使用Python从mysql数据库查询url

python - 调试类型错误: unhashable type: 'numpy.ndarray'

python - SK学习: Losing names of columns when using TfidfVectorizer

Python:获取数据框中多个数组的逐元素平均值

python - 使用给定的概率密度函数生成随机数