how can extract elements of list corresponding indices contained in 1d numpy.ndarray
?
here example:
list_data = list(range(1, 100)) arr_index = np.asarray([18, 55, 22]) arr_index.shape list_data[arr_index] # fails
i want able retrieve elements of list_data
corresponding arr_index
.
you can use numpy.take
-
import numpy np np.take(list_data,arr_index)
sample run -
in [12]: list_data = list(range(1, 20)) in [13]: list_data out[13]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] in [14]: arr_index = np.asarray([3, 5, 12]) in [15]: np.take(list_data,arr_index) out[15]: array([ 4, 6, 13])
Comments
Post a Comment