Slicing numpy array with another array

后端 未结 5 1228
青春惊慌失措
青春惊慌失措 2021-01-17 16:12

I\'ve got a large one-dimensional array of integers I need to take slices off. That\'s trivial, I\'d just do a[start:end]. The problem is that I need more of th

5条回答
  •  半阙折子戏
    2021-01-17 17:02

    Similar solution like timday. Similar speed:

    a = np.random.randint(0,20,1e6)
    start = np.random.randint(0,20,1e4)
    end = np.random.randint(0,20,1e4)
    
    def my_fun(arr,start,end):
            return arr[start:end]
    
    %timeit [my_fun(a,i[0],i[1]) for i in zip(start,end)]
    %timeit map(lambda range: a[range[0]:range[1]],zip(start,end))
    

    100 loops, best of 3: 7.06 ms per loop 100 loops, best of 3: 6.87 ms per loop

提交回复
热议问题