In normal situations a list with integers can be used as indices for an array. Let's say
arr = np.arange(10)*2
l = [1,2,5]
arr[l] # this gives np.array([2,4,10])
Instead of one list of indices, I have several, with different lenghts, an I want to get arr[l] for each sublist in my list of indices. How can I achieve this without an sequential approach (using a for), or better, using less time than using a for using numpy?
For example:
lists = [[1,2,5], [5,6], [2,8,4]]
arr = np.arange(10)*2
result = np.array([[2,4,10], [10, 12], [4,16,8]]) #this is after the procedure I want to get
resultis a NumPy array, it has dtypeobject, which is the worst possible type of NumPy array. It is an array of pointers to Python objects. Using such an array is typically slower than using a Python list.[arr[lists[k]] for k in range(len(lists))]