0

Given an array of objects:

a = numpy.array([[True,False],[True,True],[True,False,True]])

Expected output:

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

Is there a way to get this output using numpy.where()(may be with numpy.vstack() or numpy.hstack()) or some other numpy functionality that I am not aware of?

So far I've tried:

c = [numpy.hstack(numpy.where(a[i])) for i in range(len(a))]

This solves my purpose but I want to vectorize it using numpy functionalities!

PS : Don't want any loops!

stut
  • 11
  • 3
  • 1
    You can't have a numpy boolean array like that. Only an array of objects – talonmies Apr 28 '20 at 08:20
  • 1
    `a` has lists of different length; the expected ouput is also a mix of lists. (Look at `c` without the `hstack` - what is multidimensional about that?). – hpaulj Apr 28 '20 at 15:15
  • Even if I make the length of the lists( or objects) same ( say a = numpy.array([[True,False],[True,True]) and use c = (numpy.where(a)[1], I am getting c = [0,0,1] ( a flattened array). But what I want is to have c = [[0],[0,1]] (a 2d list/array) instead! How to do the split in between? @hpaulj – stut Apr 28 '20 at 18:42

0 Answers0