[Python] What is the difference between writing in one and two lines when transporting a...

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

  1. Stack

    Stack Membro Participativo

    If the replacement of elements is written in two lines, then the matrix is transported incorrectly

    x=[[i for i in list(map(int,input().split()))]
    for _ in range(int(input()))]
    print("Result:")
    for i in range(len(x)):
    for j in range(i,len(x)):
    if i>0:
    j-=1
    x[j]=x[j]
    x[j]=x[j]
    [print(*i) for i in x]



    Input: 3 1 2 3 4 5 6 7 8 9

    Result: 1 4 7 4 5 6 7 6 9

    If the replacement of elements is written in one line, then the transportation of the matrix occurs correctly

    x=[[i for i in list(map(int,input().split()))]
    for _ in range(int(input()))]
    print("Result:")
    for i in range(len(x)):
    for j in range(i,len(x)):
    x[j],x[j]=x[j],x[j]
    [print(*i) for i in x]


    Input: 3 1 2 3 4 5 6 7 8 9

    Result: 1 4 7 2 5 8 3 6 9

    Why is this happening? I don't understand the difference

    Continue reading...

Compartilhe esta Página