[Python] what and why is the difference between [:, 1] and [:, 1:2] when using...

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

  1. Stack

    Stack Membro Participativo

    import numpy as np
    a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
    col_r1 = a[:, 1]
    col_r2 = a[:, 1:2]
    print(col_r1, col_r1.shape)
    print(col_r2, col_r2.shape)


    i wonder why the output is differrent and how the difference is created.

    I asked the deepseek for help,because of the responses are Chinese characters,so it is not convenient for me to show here.

    Maybe the difference is subtle,so i don't really get what the ai wants to express.

    or my comprehension is poor,hope to find the very right answer i can grasp

    Continue reading...

Compartilhe esta Página