Note this from the Python documentation.
If you just use the assignment function you don't really get a copy but a set of pointers to data that is changed as you change the original anyway.
This excellent summary explains why it is better to use the list() function rather than using [:] for example but this only seems to work for one dimensional arrays?
"...Shallow copies of dictionaries can be made using dict.copy(), and of lists by assigning a slice of the entire list, for example, copied_list =original_list[:]..."
The slice function also needs to be modified for two dimensional arrays as well. Suggestions including importing numpy or copy to use deepcopy() etc but this seems overkill...
See this post on how to clone 2d lists
No comments:
Post a Comment