[Python] Numpy- strange behaviour of __setitem__ of array

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

  1. Stack

    Stack Membro Participativo

    Say we have an array:

    a = np.array([
    [11, 12, 13],
    [21, 22, 23],
    [31, 32, 33],
    [41, 42, 43]
    ])

    a[[1, 3], [0, 2]] = 0


    So we want to set zeros to 0th and 2nd element at both 1st and 3rd rows. But what we get is:

    [[11 12 13]
    [ 0 22 23]
    [31 32 33]
    [41 42 0]]


    Why not:

    [[11 12 13]
    [ 0 22 0]
    [31 32 33]
    [0 42 0]]


    ?

    Continue reading...

Compartilhe esta Página