如何通过创建新数组并保持旧数组不变来更新数组



我有一个类,它的名称是字符串,您可以更改它的名称(您可以看到下面的类(但当我用write函数更改Squares的名称时,它会做一些奇怪的事情:尝试更新新阵列时,它会同时更改旧阵列和新阵列

我还将包括下面的write_word函数作为代码

更新:我还尝试将.copy添加到write_word函数中,我将在下面添加新版本更新2:我还尝试将这些板创建为列表,并在列表中写入单词。我添加了create和write函数的数组和列表版本。在这两种情况下,问题都是一样的,一旦我创建了一个板并复制它并更改副本,原始和副本都会更改。这让我相信问题是由类对象而不是数组引起的。

Update3:看起来list和numpy都有同样的问题,我现在将尝试在不使用类的情况下做同样的事情。我会更新它是否工作

更新4:我根本不需要更改类,现在我的代码做了它必须做的事情,这要归功于答案。我仍然会添加在FINAL_write_word函数下工作的代码。

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

我希望这能帮助你:

old = np.arange(5)
new = old
new[0] = -1

上面的代码给出:

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

为了避免这种情况,你必须制作一个副本:

old = np.arange(5)
new = old.copy()
new[0] = -1

这给了你:

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

编辑

在您的特定情况下,您必须:

import copy

并更改线路:

temparray = crossarray

发件人:

temparray = copy.deepcopy(crossarray)

最新更新