[Python] Printing numpy matrices horizontally on the console

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    Numpy has many nice features for formatted output, but something I miss is the ability to print more than one array/matrix on the same line. What I mean is easiest to explain with an example. Given the following code:

    A = np.random.randint(10, size = (4, 4))
    B = np.random.randint(10, size = (4, 4))
    niceprint(A, "*", B, "=", A @ B)


    How can you implement niceprint so that it prints the following on the console?

    [[7 1 0 4] [[8 6 2 6] [[ 80 45 54 83]
    [8 1 5 8] * [0 3 8 5] = [147 91 123 140]
    [3 7 2 4] [7 8 7 3] [ 62 55 108 95]
    [8 8 2 8]] [6 0 8 9]] [126 88 158 166]]

    Continue reading...

Compartilhe esta Página