[英]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我有一个 class,它的名称为字符串,您可以更改其名称(您可以在下面看到 class)但是当我使用写入function 更改正方形的名称时,它会发生一些奇怪的尝试更新它:旧数组和新数组
I will also include the write_word function below as code我还将在下面包含write_word function 作为代码
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.更新:我还尝试将.copy添加到write_word function,我将在更新2下面添加新版本:我还尝试将板创建为列表,并将单词写入列表。 I added both the array and the list versions of both create and write functions.我添加了 create 和 write 函数的数组和列表版本。 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.这让我相信问题是由 class object 而不是 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. Update3:似乎列表和 numpy 都有同样的问题,我现在将尝试做同样的事情,而不使用 class。 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.更新4:我根本不需要更改 class ,现在我的代码完成了它必须做的事情,这要归功于答案。 I will still add the code that DOES work under the FINAL_write_word function.我仍将添加在 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.