Search code examples
pythonarraysnumpysparse-matrix

Given the output of numpy.where(), how to acquire a list of list of the indices


I am searching a rather large matrix for zero elements and then want to then loop over the rows and inside each row, overall the indices where I found a zero. numpy.where(M==0) gives me (np.array, np.array).

How can I format this result to my needs in the most efficient way?

# Output of numpy.where(M==0):
(x,y) = (array([0, 0, 1, 1, 2]), array([2, 3, 1, 2, 1]))
# Output I want, each value of y in the list corresponding to the x value:
wanted = [[2,3],[1,2],[1],[]]

I have thought about constructing a sparse matrix out of the output (since M is dense, M==0 should be sparse), but don't know how I would iterate over the rows there.

zeros = scipy.sparse.coo_matrix((np.ones((len(x),)), (x,y)), shape=M.shape)
print(zeros.nonzero()) # is the same output as np.where, so it does not help me

I suppose I could slice into each row and .nonzero() it, but I hope there is something more efficient

Edit: Alternatively, a dictionary of the form {x_value:[list of corresponding y_values]} would work as well


Solution

  • I found that collections.defaultdict allowed me to iterate my tuples into a dict as I wanted.

    indices = np.argwhere(M==0)
    zeros = defaultdict(list)
    for coords in indices:
        zeros[coords[0]].append(coords[1])
    print(zeros)
    
    # Output:
    defaultdict(<class 'list'>, {0: [1], 1: [1, 2], 2: [0, 3]})