简体   繁体   中英

How can I update an array by creating a new one and keeping the old one the same

I have a class that has names as strings and you can change its names (you can see the class below) but when i change the names of the Squares with the write function it does something weird: trying to update a new array it changes both the old and the new array

I will also include the write_word function below as code

Update:I also tried to add the.copy to the write_word function, Ill add the new version below Update2: I also tried to create the boards as a list, and write words into the list. I added both the array and the list versions of both create and write functions. In both of them the problem is the same, once I create a board and copy it and change the copy, both the original and the copy changes. This leads me to believe that the problem is caused by the class object and not the arrays.

Update3: it seems like both the list and numpy have the same problem, I will now try to do the same thing without using class at all. I will update whether it works or not

Update4: I didnt have to change the class at all and now my code does what it had to do thanks to the answer. I will still add the code that DOES work under the FINAL_write_word function.

class Square:
    def __init__(self, x,y):
        self.x = x
        self.y = y
        self.letter = " "
        self.num = 0
        
    def write(self, letter):
        if self.letter != "!":
            self.letter = letter.upper()
        else:
            print("cannot write on black square")
    def clear(self):
        if self.letter != "!":
            self.letter = " "
        else:
            print("cannot write on black square")
    def black(self):
        self.letter = "!"
    def isblack(self):
        if self.letter == "!":
            return True
        else:
            return False
    def isfilled(self):
        if self.letter != "!" and self.letter != " ":
            return True
        else:
            return False
    def read(self):
        return self.letter
    def number(self,num):
        self.num = num
    def getnum(self):
        return self.num
    def __str__(self):
        image = "  " + self.letter
        return image
    def __repr__(self):
        image = "  " + self.letter
        return image

def write_word(crossarray,indexes,word):
    print("now writing word:",word)
    temparray = crossarray
    for i, index in enumerate(indexes):
        row = int(index // 5)
        col = int(index % 5)
        temparray[row,col].write(word[i])
    return temparray

def UPDATED_write_word(crossarray,indexes,word):
    print("now writing word:",word)
    temparray = crossarray.copy()
    for i, index in enumerate(indexes):
        row = int(index // 5)
        col = int(index % 5)
        temparray[row,col].write(word[i])
    return temparray

def create_crossword(down,across,indexes,blacks):
    cross_array =  np.zeros((5,5), dtype=Square)
    for row in range(len(cross_array)):
        for col in range(len(cross_array[0])):
            cross_array[row,col] = Square(row,col)
    
    for name, index in enumerate(indexes):
        row = int(index // 5)
        col = int(index % 5)
        cross_array[row,col].number(name+1)
    
    for index in blacks:
        row = int(index // 5)
        col = int(index % 5)
        cross_array[row,col].black()
    return(cross_array)

def create_crosslist(down,across,indexes,blacks):
    cross_list = []
    for row in range(5):
        rowlist = []
        for col in range(5):
            rowlist.append(Square(row,col))
        cross_list.append(rowlist)
    
    for name, index in enumerate(indexes):
        row = int(index // 5)
        col = int(index % 5)
        cross_list[row][col].number(name+1)
    
    for index in blacks:
        row = int(index // 5)
        col = int(index % 5)
        cross_list[row][col].black()
    return cross_list

def write_word_list(crosslist,indexes,word):
    print("now writing word:",word)
    templist = crosslist
    for i, index in enumerate(indexes):
        row = int(index // 5)
        col = int(index % 5)
        templist[row][col].write(word[i])
    return templist

import copy
def FINAL_write_word(crossarray,indexes,word):
    print("now writing word:",word)
    temparray = copy.deepcopy(crossarray)
    for i, index in enumerate(indexes):
        row = int(index // 5)
        col = int(index % 5)
        temparray[row,col].write(word[i])
    return temparray

I hope this helps you:

old = np.arange(5)
new = old

new[0] = -1

The code above gives:

old
#array([-1,  1,  2,  3,  4])
new
#array([-1,  1,  2,  3,  4])

To avoid this you have to make a copy:

old = np.arange(5)
new = old.copy()

new[0] = -1

Which gives you:

old
#array([0, 1, 2, 3, 4])
new
#array([-1,  1,  2,  3,  4])

EDIT

In your particular case you have to:

import copy

and change the line:

temparray = crossarray

by:

temparray = copy.deepcopy(crossarray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM