从基于索引的numpy数组中获取子数组 [英] Get a subarray from a numpy array based on index
本文介绍了从基于索引的numpy数组中获取子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个numpy数组向量,我想基于索引获取一个子集:
I have a numpy array vector, and I want to get a subset based on the indexes:
import numpy as np
input=np.array([1,2,3,4,5,6,7,8,9,10])
index=np.array([0,1,0,0,0,0,1,0,0,1])
什么是python输出输出= [2,7,10]的方法?
what is a pythonic way to get out output=[2,7,10]?
推荐答案
output = input[index.astype(np.bool)]
或
output = input[np.where(index)[0]]
这篇关于从基于索引的numpy数组中获取子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文