[Python] Alternative to looping over one numpy axis

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

  1. Stack

    Stack Membro Participativo

    I have two numpy arrays a and b such that a.shape[:-1] and b.shape are broadcastable. With this constraint only, I want to calculate an array c according to the following:

    c = numpy.empty(numpy.broadcast_shapes(a.shape[:-1],b.shape),a.dtype)
    for i in range(a.shape[-1]):
    c[...,i] = a[...,i] * b


    The above code certainly works, but I would like to know if there is a more elegant (and idiomatic) way of doing it.

    Continue reading...

Compartilhe esta Página