1

I am trying to use a recursive function in Python. Even if I can change the value of a global value with a modifier, it re-changes when this modifier change.

Here is the code scheme;

array = [1,2,3]
middle_array = []
last_array = []

def recursive_array(in_array):
    global last_array
    global middle_array

    for rank in range(0, len(in_array)):

        if len(in_array) == 0:
            last_array.append(middle_array)
            return last_array
        elif len(in_array) == 1:
            middle_array.append(in_array[0])
            last_array.append(middle_array)
            return last_array

        middle_array.append(in_array[rank])

        bottom = rank -1
        top = rank +2

        if bottom <= 0:
            bottom = 0
        if top >= len(in_array):
            top = len(in_array)

        changable_array = in_array[top:]

        recursive_array(changable_array)

        last_array.append(middle_array)

        middle_array.pop()

    return last_array

First "recursive_array" cycle starts with [1,2,3] "in_array". "middle_array" take correct append() command and programme execute correct "changeable_array" as [3].

The first call of recursive comes with "in_array" as [3] so goes to "elif len(in_array) == 1:" command. Updating is done correctly. Now, middle_array is [1,3] and last_array is [1,3] then returning to add last append to last_array as [[1,3],[1,3]].

     elif len(in_array) == 1:
        middle_array.append(in_array[0])
        last_array.append(middle_array)

After this, the command "middle_array.pop()" command is removing last element of the array. However, here is my problem; when the middle_array poped, "last_array" values also pops.

    recursive_array(changable_array)

    last_array.append(middle_array)

    middle_array.pop()

Before pop;

last_array = [[1,3],[1,3]] middle_array = [1,3]

After pop;

last_array = [[1],[1]] middle_array = [1]

I want to pop the value in middle_array but keep the values in the last_array. Could you show me the path? Thanks for sparing time and your interest.

wwii
  • 23,232
  • 7
  • 37
  • 77
anejo
  • 11
  • 2
  • 2
    There are multiple references to the same list object, changing one changes all. Use e. g. `middle_array[:]` to create a (shallow) copy of the list which you can append to `last_array`. – Michael Butscher Jun 12 '20 at 23:29
  • `last_array` contains two references to `middle_array`. When you print `last_array`, these references are followed and so include any changes made to `middle_array`. What you might consider doing is calling `copy()` on `middle_array` so that `last_array` does not contain references directly to it, but instead to a different data structure containing a replica of the data. – Jamie Jun 12 '20 at 23:30
  • [This example](https://stackoverflow.com/questions/6360286/why-does-foo-appendbar-affect-all-elements-in-a-list-of-lists) for this referencing confusion is a bit simpler and might help you understand the issue. – David Wierichs Jun 12 '20 at 23:30
  • Add `print([thing is middle_array for thing in last_array])` after `middle_array.pop()` to see that all the items in last_array are the same thing. ... [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html) – wwii Jun 12 '20 at 23:35
  • Copy() method is solved, same as other methods I suppose, thanks a lot. The issue is related to differences between value and reference storage I suppose. Quick answers and comments that I didn't expect. THANKS AGAIN. – anejo Jun 12 '20 at 23:49

1 Answers1

1

I want to pop the value in middle_array but keep the values in the last_array

Whenever you append middle_array (two places that I can see) append a copy:

        last_array.append(middle_array.copy())
wwii
  • 23,232
  • 7
  • 37
  • 77
  • It solved the issue, thanks a lot. I noticed and remembered the difference between value and reference, I suppose it is related to. Quick answers and comments that I didn't think. THANKS AGAIN. – anejo Jun 12 '20 at 23:47