1

So, in my program I have a "main" function, which changes two elements of a given matrix. The matrix is an element of a list (in the example the list is the variable solved) and then I want to append three new elements.

def main(matrix,direction):
    index16 = indexOf(16,matrix)
    matrix[index16[0]][index16[1]],matrix[index16[0]-1][index16[1]]=matrix[index16[0]-1][index16[1]],matrix[index16[0]][index16[1]]

    return matrix

solved = [[[2,1,3,4],
      [5,6,7,8],
      [9,10,11,12],
      [13,14,15,16]
      ]]

not_solved = [[0,"up"],
          [0,"left"]
          ]

while not_solved:
    solved.append(main(solved[not_solved[0][0]],not_solved[0][1]))
break

When I execute the program, I can see the "solved" array. However the initial matrix stays the same as in the beginning.

[[[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 16], [13, 14, 15, 12]], 
 [[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 16], [13, 14, 15, 12]]]

How can I repair that?

Sorry for my English. I am still learning.

8
  • 1
    Can u add the expected output please? Commented Sep 18, 2015 at 15:54
  • have you also defined indexOf somewhere? Commented Sep 18, 2015 at 16:01
  • Ahsanul it is threre, Commented Sep 18, 2015 at 16:02
  • tom, I added the function there Commented Sep 18, 2015 at 16:03
  • 1
    So solved is a list of matrices. The first matrix in the list should have stayed as it was, and a new solution appended. But the first matrix was changed to be the same as the second one. It's presumably a question of pointer vs copy Commented Sep 18, 2015 at 16:09

2 Answers 2

1

you need to copy your matrix inside main so the original matrix does not change

import copy

def main(matrix,direction):
    matrixcopy = copy.deepcopy(matrix)
    index16 = indexOf(16,matrixcopy)
    matrixcopy[index16[0]][index16[1]],matrixcopy[index16[0]-1][index16[1]]=matrixcopy[index16[0]-1][index16[1]],matrixcopy[index16[0]][index16[1]]
    return matrixcopy

Returns:

[[[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 
 [[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 16], [13, 14, 15, 12]]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you very much.
0

The problem is your main function

def main(matrix,direction):
    index16 = indexOf(16,matrix)
    matrix[index16[0]][index16[1]],matrix[index16[0]-1][index16[1]]=matrix[index16[0]-1][index16[1]],matrix[index16[0]][index16[1]]

    return matrix

In this function you're returning matrix, but you're also changing matrix, which is your original matrix.

Consider this simple example:

>>> a=[1,2,3]
>>> def test(b):
    b[1]=4
    return b
>>> c = test(a)
>>> c
[1, 4, 3]
>>> a
[1, 4, 3]

A possible solution is to use the copy module

>>> import copy
>>> a=[1,2,3]
>>> def test(b):
    c=copy.deepcopy(b)
    c[1]=4
    return c

>>> c = test(a)
>>> c
[1, 4, 3]
>>> a
[1, 2, 3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.