[Python] Shifting the elements of an array in python

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

  1. Stack

    Stack Membro Participativo

    I am trying to shift the elements of an array cyclically so all elements are replaced with the previous element, and the last rotates to the first poaition, like so:

    shift(1, [5, 6, 7]) => [7, 5, 6]


    The following code only returns [7,5]. Could someone please tell me what is causing this to happen? I went through the code step by step and simply could not find a solution. I also tried 3 different interpreters.

    def shift(key, array):
    counter = range(len(array)-1)
    new = counter
    for i in counter:
    new = array[i-key]
    return new

    print shift(1, [5, 6, 7])

    Continue reading...

Compartilhe esta Página