[Python] why can't I swap elements in my python list?

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

  1. Stack

    Stack Membro Participativo

    I'm making a simple animation sequence to show how a sorting algorithm works visually (Selection Sort). When I want to update my list by swapping the elements, the list won't change no matter what I try. I know python lists are mutable, so what gives? I am only changing the integer list, then filling an empty list of Rect()'s with what I wish were the swapped values after the swap animation plays. I have tried the tuple assignment syntax: arr[0], arr[1] = arr[1], arr[0] and also the traditional temp variable syntax, and neither worked. My list is initialized outside of the game loop (while running), but inside the main() method.

    I am getting my indices from a dict object that captures the smallest integer in the list and the swap target integer in a pygame.CUSTOMEVENT that I stop to run the swap animation, then continue to start the next iteration of the inside loop.

    The indices are correct when I use print() to see their location in the list.

    Just wondering if there's something in pygame that I am missing, or has anyone else experienced this issue? I am quite stuck.

    If I actually get a response I can make a small pygame example to explain with code.

    Continue reading...

Compartilhe esta Página