1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Numpy.where on an array of strings using regex

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have an array like:

    array = ['A0','A1','A2','A3','A4','B0','B1','C0']


    and want to obtain an array which is true for values with an A followed by a number ranging from 0 to 2.

    So far, this is the way I do it:

    selection = np.where ((array == 'A0') | (array == 'A1') | (array == 'A2'), 1, 0)


    But is there a more elegant way to do this by using e.g., a regular expresion like:

    selection = np.where (array == 'A[0-1]', 1, 0)

    Continue reading...

Compartilhe esta Página