簡體   English   中英

從基於索引的numpy數組中獲取子數組

[英]Get a subarray from a numpy array based on index

我有一個numpy數組向量,我想得到一個基於索引的子集:

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])

什么是pythonic方法來輸出輸出= [2,7,10]?

output = input[index.astype(np.bool)]

要么

output = input[np.where(index)[0]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM