1

How to create a list that can not change its values, for example, i have that for loop in java:

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
                if (i != j) {
                    List<Integer> vizinhoAplicacao = new ArrayList<>(ordemAcoes); //HERE
                    vizinhoAplicacao.set(i, vizinhoAplicacao.get(i) + 5);
                    vizinhoAplicacao.set(j, vizinhoAplicacao.get(j) - 5);
                    calcularRetornoAnual(vizinhoAplicacao, contatorVizinho, numeroSimulacao);
                    contatorVizinho++;
                }
            }
        }

The value of ordemAcoes always stays the same as it goes into the loop, But when I make the same scenario using python, the ordemAcoes changes as I make changes to the object I declare using it, what I want to do is not to change the value of ordemAcoes, so that every time I enter the loop, I has the same values as before.

    for i in range(0, 5):
        for j in range(0, 5):
            if (i != j):
                neighborAplication = actionOrder #HERE is the list
                neighborAplication[i] = neighborAplication[i] + 5
                neighborAplication[j] = neighborAplication[j] - 5
                returnCalculateY(neighborAplication, countNeighbor, simulationNumber)
                countNeighbor += 1

Result in Java:

[35, 20, 20, 15, 10]
[35, 25, 15, 15, 10]
[35, 25, 20, 10, 10]
[35, 25, 20, 15, 5]

Result in Python:

[35, 20, 20, 15, 10]
[40, 20, 15, 15, 10]
[45, 20, 15, 10, 10]
[50, 20, 15, 10, 5]

I want to have the same result also in python, however the list actionOrder value changes when I use neighborAplication.

Jefferson Bruchado
  • 888
  • 2
  • 14
  • 33
  • 1
    Longshot: does `neighborAplication = actionOrder[:]` fix it? – timgeb Nov 03 '18 at 17:27
  • I'll try this option thankss! – Jefferson Bruchado Nov 03 '18 at 17:30
  • I'll write an answer explaining why this fixes the problem - IF it fixes the problem. :) – timgeb Nov 03 '18 at 17:30
  • 2
    The problem is that you create new list in Java by doing `new ArrayList<>(ordemAcoes)`. When you mutate this list, it won't touch `ordemAcoes`. In Python, you just assign your list to a new variable. So when you mutate `neighborAplication`, you are actually mutating the same list as `actionOrder`. You therefore need to create a copy of the `actionOrder` list before mutating it. See this post for how to fix it in Python: https://stackoverflow.com/a/2612815/4137489 – marstran Nov 03 '18 at 17:32
  • Yes, I managed to understand this relationship, thank you, the option that the timgeb gave worked perfectly !! – Jefferson Bruchado Nov 03 '18 at 17:33

1 Answers1

1

The problem is that with

neighborAplication = actionOrder

you are just creating another reference with the name neighborAplication, and
assignment never copies data. When you mutate neighborAplication or actionOrder, the change will be seen across both names, because in-memory there only exists one list.

Use

neighborAplication = actionOrder[:]

in order to create a (shallow) copy of your list.

See How to clone or copy a list? for additional details.

timgeb
  • 76,762
  • 20
  • 123
  • 145