Fast way to select n items (drawn from a Poisson distribution) for each element in array x

后端 未结 2 2031
野性不改
野性不改 2021-01-18 23:12

I am having some trouble with solving a problem I encountered.

I have an array with prices:

>>> x = np.random.randint(10, size=10)
array([6,         


        
2条回答
  •  盖世英雄少女心
    2021-01-18 23:46

    I don't really see how you could do that without looping at all. What you could do is create the result array prior to looping; that way you don't need to concatenate afterwards.

    Result = np.empty( arrivals.sum(), dtype='i' )
    

    and then change the values of that array blockwise:

    Result_position = np.r_[ [0], arrivals.cumsum() ]
    for i, xx in enumerate(x):
        Result[ Result_position[i]:Result_position[i+1] ] = xx
    

提交回复
热议问题