Retrieving array elements with an array of frequencies in NumPy

前端 未结 3 1619
故里飘歌
故里飘歌 2021-01-22 08:21

I have an array of numbers, a. I have a second array, b, specifying how many times I want to retrieve the corresponding element in a. How

相关标签:
3条回答
  • 2021-01-22 08:48

    Thats exactly what np.arange(5).repeat([1,0,3,2,0]) does.

    0 讨论(0)
  • 2021-01-22 08:51

    A really inefficient way to do that is this one :

    import numpy as np
    
    a = np.arange(5)
    b = np.array([1,0,3,2,0])
    
    res = []
    i = 0
    for val in b:
        for aa in range(val):
            res.append(a[i])
        i += 1
    print res
    
    0 讨论(0)
  • 2021-01-22 09:14

    here's one way to do it:

    res = []
    for i in xrange(len(b)):
        for j in xrange(b[i]):
            out.append(a[i])
    
    res = np.array(res)  # optional
    
    0 讨论(0)
提交回复
热议问题