Variable change unexpectedly on recursion?

后端 未结 1 938
猫巷女王i
猫巷女王i 2021-01-24 08:59

Context

I\'m currently attempting Reddit\'s /r/dailyprogrammer challenge.

The idea is to find a solution to an ASCII maze. Unfortunately the recursion is worki

相关标签:
1条回答
  • 2021-01-24 09:52

    newMaze = maze doesn't copy the list, it just creates another name pointing to the same object. To copy, you should import copy at the top of your program, then do newMaze = copy.deepcopy(maze). (You need a deep copy because maze is a list of lists, so you need to copy not only the outer list, but all the lists inside it too.)

    In Python, assignment to a plain name (like blah = ...) never copies anything. If you want a copy, you must make one explicitly. The way to do that depends on what you're copying.

    0 讨论(0)
提交回复
热议问题