[Python] np.where with a in-type condition

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    Given a numpy array

    arr = np.array([1, 2, 3, 4, 5])


    I need to construct a binary mask according to a (arbitrary, potentially long) list of values, i.e. given

    values = np.array([2, 4, 5])


    mask should be

    mask = np.array([False, True, False, True, True])


    So I want to avoid

    condition = (arr==2) or (arr==4) or (arr==5)
    mask = np.where(condition, arr)


    to get something like

    mask = np.in(values, arr)


    Or, if it is not possible, how to construct a condition from an arbitrary list of values to feed into np.where?

    Continue reading...

Compartilhe esta Página