List value swapping: What's the correct order and why?












2















For a list of integers, such as A = [2, 10, -5], I get the error



Traceback (most recent call last):
File "so.py", line 6, in <module>
v, A[v-1] = A[v-1], v
IndexError: list assignment index out of range


Code:



for i, v in enumerate(A):
while 1<=v<=len(A) and v != A[v-1]:
v, A[v-1] = A[v-1], v


but this works:



for i, v in enumerate(A):
while 1<=v<=len(A) and v != A[v-1]:
A[v-1], v = v, A[v-1]


Why the order of the swapping elements matters here? v is always being checked to be in bound.



Weirdly enough cannot reproduce a smaller example. But,
A = [6, 5, 4, 3, 2]
becomes an infinite loop.










share|improve this question




















  • 4





    Can you provide A and n?

    – Ssein
    Nov 15 '18 at 17:29






  • 1





    You should refer to this post.

    – Dimeji Mudele
    Nov 15 '18 at 17:41











  • Don't reassign the iteration variable, v.

    – hpaulj
    Nov 15 '18 at 17:55






  • 1





    i is the index. v the value. A[i] makes sense. A[v] does not.

    – hpaulj
    Nov 15 '18 at 17:58











  • Note that your inner loop is a while, not an if. Your list [6, 5, 4, 3, 2] gets stuck in the while.

    – Prune
    Nov 15 '18 at 18:10
















2















For a list of integers, such as A = [2, 10, -5], I get the error



Traceback (most recent call last):
File "so.py", line 6, in <module>
v, A[v-1] = A[v-1], v
IndexError: list assignment index out of range


Code:



for i, v in enumerate(A):
while 1<=v<=len(A) and v != A[v-1]:
v, A[v-1] = A[v-1], v


but this works:



for i, v in enumerate(A):
while 1<=v<=len(A) and v != A[v-1]:
A[v-1], v = v, A[v-1]


Why the order of the swapping elements matters here? v is always being checked to be in bound.



Weirdly enough cannot reproduce a smaller example. But,
A = [6, 5, 4, 3, 2]
becomes an infinite loop.










share|improve this question




















  • 4





    Can you provide A and n?

    – Ssein
    Nov 15 '18 at 17:29






  • 1





    You should refer to this post.

    – Dimeji Mudele
    Nov 15 '18 at 17:41











  • Don't reassign the iteration variable, v.

    – hpaulj
    Nov 15 '18 at 17:55






  • 1





    i is the index. v the value. A[i] makes sense. A[v] does not.

    – hpaulj
    Nov 15 '18 at 17:58











  • Note that your inner loop is a while, not an if. Your list [6, 5, 4, 3, 2] gets stuck in the while.

    – Prune
    Nov 15 '18 at 18:10














2












2








2








For a list of integers, such as A = [2, 10, -5], I get the error



Traceback (most recent call last):
File "so.py", line 6, in <module>
v, A[v-1] = A[v-1], v
IndexError: list assignment index out of range


Code:



for i, v in enumerate(A):
while 1<=v<=len(A) and v != A[v-1]:
v, A[v-1] = A[v-1], v


but this works:



for i, v in enumerate(A):
while 1<=v<=len(A) and v != A[v-1]:
A[v-1], v = v, A[v-1]


Why the order of the swapping elements matters here? v is always being checked to be in bound.



Weirdly enough cannot reproduce a smaller example. But,
A = [6, 5, 4, 3, 2]
becomes an infinite loop.










share|improve this question
















For a list of integers, such as A = [2, 10, -5], I get the error



Traceback (most recent call last):
File "so.py", line 6, in <module>
v, A[v-1] = A[v-1], v
IndexError: list assignment index out of range


Code:



for i, v in enumerate(A):
while 1<=v<=len(A) and v != A[v-1]:
v, A[v-1] = A[v-1], v


but this works:



for i, v in enumerate(A):
while 1<=v<=len(A) and v != A[v-1]:
A[v-1], v = v, A[v-1]


Why the order of the swapping elements matters here? v is always being checked to be in bound.



Weirdly enough cannot reproduce a smaller example. But,
A = [6, 5, 4, 3, 2]
becomes an infinite loop.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:19









martineau

69.1k1092186




69.1k1092186










asked Nov 15 '18 at 17:22









kaiserasifkaiserasif

588




588








  • 4





    Can you provide A and n?

    – Ssein
    Nov 15 '18 at 17:29






  • 1





    You should refer to this post.

    – Dimeji Mudele
    Nov 15 '18 at 17:41











  • Don't reassign the iteration variable, v.

    – hpaulj
    Nov 15 '18 at 17:55






  • 1





    i is the index. v the value. A[i] makes sense. A[v] does not.

    – hpaulj
    Nov 15 '18 at 17:58











  • Note that your inner loop is a while, not an if. Your list [6, 5, 4, 3, 2] gets stuck in the while.

    – Prune
    Nov 15 '18 at 18:10














  • 4





    Can you provide A and n?

    – Ssein
    Nov 15 '18 at 17:29






  • 1





    You should refer to this post.

    – Dimeji Mudele
    Nov 15 '18 at 17:41











  • Don't reassign the iteration variable, v.

    – hpaulj
    Nov 15 '18 at 17:55






  • 1





    i is the index. v the value. A[i] makes sense. A[v] does not.

    – hpaulj
    Nov 15 '18 at 17:58











  • Note that your inner loop is a while, not an if. Your list [6, 5, 4, 3, 2] gets stuck in the while.

    – Prune
    Nov 15 '18 at 18:10








4




4





Can you provide A and n?

– Ssein
Nov 15 '18 at 17:29





Can you provide A and n?

– Ssein
Nov 15 '18 at 17:29




1




1





You should refer to this post.

– Dimeji Mudele
Nov 15 '18 at 17:41





You should refer to this post.

– Dimeji Mudele
Nov 15 '18 at 17:41













Don't reassign the iteration variable, v.

– hpaulj
Nov 15 '18 at 17:55





Don't reassign the iteration variable, v.

– hpaulj
Nov 15 '18 at 17:55




1




1





i is the index. v the value. A[i] makes sense. A[v] does not.

– hpaulj
Nov 15 '18 at 17:58





i is the index. v the value. A[i] makes sense. A[v] does not.

– hpaulj
Nov 15 '18 at 17:58













Note that your inner loop is a while, not an if. Your list [6, 5, 4, 3, 2] gets stuck in the while.

– Prune
Nov 15 '18 at 18:10





Note that your inner loop is a while, not an if. Your list [6, 5, 4, 3, 2] gets stuck in the while.

– Prune
Nov 15 '18 at 18:10












3 Answers
3






active

oldest

votes


















2














I reproduced this with [2, 10, -5].
The detailed sequence of operations is



i, v = 0, 2
1 <= 2 ? OK
2 != A[1] ? OK ... stay in loop
v, A[v-1] = 10, 2
# This assignment breaks into ...
v = 10
A[10-1] = 2
# ... and this second assignment is out of range.


If you switch the assignment order:



    A[v-1], v = 2, 10
# This assignment breaks into ...
A[2-1] = 2
v = 10


In this case, your while conditions have properly guarded a legal assignment.



Note that you are not swapping list values; v is a local variable; it is not a reference to A[i]. For instance, in the above example, you do get v=10, but this does not affect the value of A[0].






share|improve this answer

































    7














    Python swaps the variables in the order provided, so v is assigned the value at A[v-1], and then tries to reassign A[v-1] - but since v has been modified to be a list element, v-1 is out of range of A.






    share|improve this answer
























    • Then, why the opposite doesn't cause the problem, i.e. A[v-1] set to v first, then v still gets the previous A[v-1] value from before the update.

      – kaiserasif
      Nov 15 '18 at 17:44






    • 1





      Because you have an if statement ensuring that v is less than the length of the array, so A[v-1] is always set to a value that can index A in the next step.

      – Tim
      Nov 15 '18 at 17:57



















    0














    A = [6, 5, 4, 3, 2]

    for i, v in enumerate(A):
    while 1<=v<=len(A) and v != A[v-1]:
    v, A[v-1] = A[v-1], v


    You need to try running out your algorithm in your head:



    Start:



    i = 0, v = 6



    v(6) is not between 1 and 5: next iteration



    i = 1, v = 5, A[5-1] = 2



    5 is between 1 and 5; v is not equal to 2: swap ->
    v = 2; A[4] = 2



    i = 1, v = 2, A[2-1] = 5



    2 is between 1 and 5; v is not equal to 5: swap ->
    v = 5; A[1] = 5



    i = 1, v = 5, A[5-1] = 2



    5 is between 1 and 5; v is not equal to 2: swap ->
    v = 2; A[4] = 2



    i = 1, v = 2, A[2-1] = 5



    2 is between 1 and 5; v is not equal to 5: swap ->
    v = 5; A[1] = 5



    ... and on and on



    I don't think your algorithm makes sense. It's unclear why you are using the values in your list to index the list during your loop. I think this confusion about the index and values is at the root of your problem.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324839%2flist-value-swapping-whats-the-correct-order-and-why%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      I reproduced this with [2, 10, -5].
      The detailed sequence of operations is



      i, v = 0, 2
      1 <= 2 ? OK
      2 != A[1] ? OK ... stay in loop
      v, A[v-1] = 10, 2
      # This assignment breaks into ...
      v = 10
      A[10-1] = 2
      # ... and this second assignment is out of range.


      If you switch the assignment order:



          A[v-1], v = 2, 10
      # This assignment breaks into ...
      A[2-1] = 2
      v = 10


      In this case, your while conditions have properly guarded a legal assignment.



      Note that you are not swapping list values; v is a local variable; it is not a reference to A[i]. For instance, in the above example, you do get v=10, but this does not affect the value of A[0].






      share|improve this answer






























        2














        I reproduced this with [2, 10, -5].
        The detailed sequence of operations is



        i, v = 0, 2
        1 <= 2 ? OK
        2 != A[1] ? OK ... stay in loop
        v, A[v-1] = 10, 2
        # This assignment breaks into ...
        v = 10
        A[10-1] = 2
        # ... and this second assignment is out of range.


        If you switch the assignment order:



            A[v-1], v = 2, 10
        # This assignment breaks into ...
        A[2-1] = 2
        v = 10


        In this case, your while conditions have properly guarded a legal assignment.



        Note that you are not swapping list values; v is a local variable; it is not a reference to A[i]. For instance, in the above example, you do get v=10, but this does not affect the value of A[0].






        share|improve this answer




























          2












          2








          2







          I reproduced this with [2, 10, -5].
          The detailed sequence of operations is



          i, v = 0, 2
          1 <= 2 ? OK
          2 != A[1] ? OK ... stay in loop
          v, A[v-1] = 10, 2
          # This assignment breaks into ...
          v = 10
          A[10-1] = 2
          # ... and this second assignment is out of range.


          If you switch the assignment order:



              A[v-1], v = 2, 10
          # This assignment breaks into ...
          A[2-1] = 2
          v = 10


          In this case, your while conditions have properly guarded a legal assignment.



          Note that you are not swapping list values; v is a local variable; it is not a reference to A[i]. For instance, in the above example, you do get v=10, but this does not affect the value of A[0].






          share|improve this answer















          I reproduced this with [2, 10, -5].
          The detailed sequence of operations is



          i, v = 0, 2
          1 <= 2 ? OK
          2 != A[1] ? OK ... stay in loop
          v, A[v-1] = 10, 2
          # This assignment breaks into ...
          v = 10
          A[10-1] = 2
          # ... and this second assignment is out of range.


          If you switch the assignment order:



              A[v-1], v = 2, 10
          # This assignment breaks into ...
          A[2-1] = 2
          v = 10


          In this case, your while conditions have properly guarded a legal assignment.



          Note that you are not swapping list values; v is a local variable; it is not a reference to A[i]. For instance, in the above example, you do get v=10, but this does not affect the value of A[0].







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 18:06

























          answered Nov 15 '18 at 17:56









          PrunePrune

          45.2k143559




          45.2k143559

























              7














              Python swaps the variables in the order provided, so v is assigned the value at A[v-1], and then tries to reassign A[v-1] - but since v has been modified to be a list element, v-1 is out of range of A.






              share|improve this answer
























              • Then, why the opposite doesn't cause the problem, i.e. A[v-1] set to v first, then v still gets the previous A[v-1] value from before the update.

                – kaiserasif
                Nov 15 '18 at 17:44






              • 1





                Because you have an if statement ensuring that v is less than the length of the array, so A[v-1] is always set to a value that can index A in the next step.

                – Tim
                Nov 15 '18 at 17:57
















              7














              Python swaps the variables in the order provided, so v is assigned the value at A[v-1], and then tries to reassign A[v-1] - but since v has been modified to be a list element, v-1 is out of range of A.






              share|improve this answer
























              • Then, why the opposite doesn't cause the problem, i.e. A[v-1] set to v first, then v still gets the previous A[v-1] value from before the update.

                – kaiserasif
                Nov 15 '18 at 17:44






              • 1





                Because you have an if statement ensuring that v is less than the length of the array, so A[v-1] is always set to a value that can index A in the next step.

                – Tim
                Nov 15 '18 at 17:57














              7












              7








              7







              Python swaps the variables in the order provided, so v is assigned the value at A[v-1], and then tries to reassign A[v-1] - but since v has been modified to be a list element, v-1 is out of range of A.






              share|improve this answer













              Python swaps the variables in the order provided, so v is assigned the value at A[v-1], and then tries to reassign A[v-1] - but since v has been modified to be a list element, v-1 is out of range of A.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 17:35









              TimTim

              1,772621




              1,772621













              • Then, why the opposite doesn't cause the problem, i.e. A[v-1] set to v first, then v still gets the previous A[v-1] value from before the update.

                – kaiserasif
                Nov 15 '18 at 17:44






              • 1





                Because you have an if statement ensuring that v is less than the length of the array, so A[v-1] is always set to a value that can index A in the next step.

                – Tim
                Nov 15 '18 at 17:57



















              • Then, why the opposite doesn't cause the problem, i.e. A[v-1] set to v first, then v still gets the previous A[v-1] value from before the update.

                – kaiserasif
                Nov 15 '18 at 17:44






              • 1





                Because you have an if statement ensuring that v is less than the length of the array, so A[v-1] is always set to a value that can index A in the next step.

                – Tim
                Nov 15 '18 at 17:57

















              Then, why the opposite doesn't cause the problem, i.e. A[v-1] set to v first, then v still gets the previous A[v-1] value from before the update.

              – kaiserasif
              Nov 15 '18 at 17:44





              Then, why the opposite doesn't cause the problem, i.e. A[v-1] set to v first, then v still gets the previous A[v-1] value from before the update.

              – kaiserasif
              Nov 15 '18 at 17:44




              1




              1





              Because you have an if statement ensuring that v is less than the length of the array, so A[v-1] is always set to a value that can index A in the next step.

              – Tim
              Nov 15 '18 at 17:57





              Because you have an if statement ensuring that v is less than the length of the array, so A[v-1] is always set to a value that can index A in the next step.

              – Tim
              Nov 15 '18 at 17:57











              0














              A = [6, 5, 4, 3, 2]

              for i, v in enumerate(A):
              while 1<=v<=len(A) and v != A[v-1]:
              v, A[v-1] = A[v-1], v


              You need to try running out your algorithm in your head:



              Start:



              i = 0, v = 6



              v(6) is not between 1 and 5: next iteration



              i = 1, v = 5, A[5-1] = 2



              5 is between 1 and 5; v is not equal to 2: swap ->
              v = 2; A[4] = 2



              i = 1, v = 2, A[2-1] = 5



              2 is between 1 and 5; v is not equal to 5: swap ->
              v = 5; A[1] = 5



              i = 1, v = 5, A[5-1] = 2



              5 is between 1 and 5; v is not equal to 2: swap ->
              v = 2; A[4] = 2



              i = 1, v = 2, A[2-1] = 5



              2 is between 1 and 5; v is not equal to 5: swap ->
              v = 5; A[1] = 5



              ... and on and on



              I don't think your algorithm makes sense. It's unclear why you are using the values in your list to index the list during your loop. I think this confusion about the index and values is at the root of your problem.






              share|improve this answer




























                0














                A = [6, 5, 4, 3, 2]

                for i, v in enumerate(A):
                while 1<=v<=len(A) and v != A[v-1]:
                v, A[v-1] = A[v-1], v


                You need to try running out your algorithm in your head:



                Start:



                i = 0, v = 6



                v(6) is not between 1 and 5: next iteration



                i = 1, v = 5, A[5-1] = 2



                5 is between 1 and 5; v is not equal to 2: swap ->
                v = 2; A[4] = 2



                i = 1, v = 2, A[2-1] = 5



                2 is between 1 and 5; v is not equal to 5: swap ->
                v = 5; A[1] = 5



                i = 1, v = 5, A[5-1] = 2



                5 is between 1 and 5; v is not equal to 2: swap ->
                v = 2; A[4] = 2



                i = 1, v = 2, A[2-1] = 5



                2 is between 1 and 5; v is not equal to 5: swap ->
                v = 5; A[1] = 5



                ... and on and on



                I don't think your algorithm makes sense. It's unclear why you are using the values in your list to index the list during your loop. I think this confusion about the index and values is at the root of your problem.






                share|improve this answer


























                  0












                  0








                  0







                  A = [6, 5, 4, 3, 2]

                  for i, v in enumerate(A):
                  while 1<=v<=len(A) and v != A[v-1]:
                  v, A[v-1] = A[v-1], v


                  You need to try running out your algorithm in your head:



                  Start:



                  i = 0, v = 6



                  v(6) is not between 1 and 5: next iteration



                  i = 1, v = 5, A[5-1] = 2



                  5 is between 1 and 5; v is not equal to 2: swap ->
                  v = 2; A[4] = 2



                  i = 1, v = 2, A[2-1] = 5



                  2 is between 1 and 5; v is not equal to 5: swap ->
                  v = 5; A[1] = 5



                  i = 1, v = 5, A[5-1] = 2



                  5 is between 1 and 5; v is not equal to 2: swap ->
                  v = 2; A[4] = 2



                  i = 1, v = 2, A[2-1] = 5



                  2 is between 1 and 5; v is not equal to 5: swap ->
                  v = 5; A[1] = 5



                  ... and on and on



                  I don't think your algorithm makes sense. It's unclear why you are using the values in your list to index the list during your loop. I think this confusion about the index and values is at the root of your problem.






                  share|improve this answer













                  A = [6, 5, 4, 3, 2]

                  for i, v in enumerate(A):
                  while 1<=v<=len(A) and v != A[v-1]:
                  v, A[v-1] = A[v-1], v


                  You need to try running out your algorithm in your head:



                  Start:



                  i = 0, v = 6



                  v(6) is not between 1 and 5: next iteration



                  i = 1, v = 5, A[5-1] = 2



                  5 is between 1 and 5; v is not equal to 2: swap ->
                  v = 2; A[4] = 2



                  i = 1, v = 2, A[2-1] = 5



                  2 is between 1 and 5; v is not equal to 5: swap ->
                  v = 5; A[1] = 5



                  i = 1, v = 5, A[5-1] = 2



                  5 is between 1 and 5; v is not equal to 2: swap ->
                  v = 2; A[4] = 2



                  i = 1, v = 2, A[2-1] = 5



                  2 is between 1 and 5; v is not equal to 5: swap ->
                  v = 5; A[1] = 5



                  ... and on and on



                  I don't think your algorithm makes sense. It's unclear why you are using the values in your list to index the list during your loop. I think this confusion about the index and values is at the root of your problem.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 18:07









                  vaer-kvaer-k

                  3,94852335




                  3,94852335






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324839%2flist-value-swapping-whats-the-correct-order-and-why%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values