简体   繁体   中英

Efficient way to index a list with a numpy bool array in Python

如标题所示,索引具有(n,)布尔ndarray arlen(l) == ar.shape[0]list l的最有效方法是什么?

Ok, let's make a list of idioms you could try. Time them you'll have to do yourself, because it will depend on the sizes and also the sparsity of ar , cf. @Imanol Luengo's comment

np.array(l)[ar] # output is an array
[e for e,m in zip(l, ar) if m]
[l[i] for i in np.where(ar)[0]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM