Python function to find indexes of 1s in binary array and

前端 未结 3 989
走了就别回头了
走了就别回头了 2021-01-21 07:45

I have an array which looks like this

[1, 0, 1 , 0 , 0, 1]

And I want to get those indexes that have 1 in it. So here I would get an array of <

3条回答
  •  梦谈多话
    2021-01-21 08:04

    Here's one compact way -

    2**np.where(a)[0]
    

    Sample run -

    In [83]: a = np.array([1, 0, 1 , 0 , 0, 1])
    
    In [84]: 2**np.where(a)[0]
    Out[84]: array([ 1,  4, 32])
    

提交回复
热议问题