[英]Fill a numpy array with objects
我的代码有时会引发AttributeError
。 确切的错误是: AttributeError: 'int' object has no attribute 'place'
我用a=np.eye(9, dtype=object)
创建一个numpy
数组,然后用对象填充它,如下所示:
for i in range(9): #rows
#Create the initial Place for each row in the column 0
j=0
#Check if there is something already stored if not store a new object
try:
places[i,j].place #An attribute of the
except:
places[i,j]=Place() #Where Place() is the object (it has the attribute place)
for j in range(9): #columns
#For the other columns of the first row(i==0) create a Place according to the previous Place
if i==0 and j>=1:
#If there is something already don't create a new Place
try:
places[i,j].place
except AttributeError:
if places[i,j-1].place=='cave':
places[i,j]=Place()
if i>=1 and j>=1:
try:
places[i,j].place
except:
print(places)
if places[i,j-1].place=='cave' and places[i-1,j].place=='cave':
places[i,j]=Place()
当我运行此代码时,它通常在以下情况下最后停止: if places[i,j-1].place=='cave' and places[i-1,j].place=='cave':
但是它应该可以正常工作,不是吗?
我放入了print(places)
来检查此过程如何运行以及如何填充。 这是第一次运行的结果(它具有先前条件的其他一些对象与该问题无关,或者也许与该问题无关):
[[<probe_2.Place object at 0x03920690> <probe_2.Place object at 0x038C4F70>
<probe_2.Place object at 0x03912B70> <probe_2.Place object at 0x039083F0>
<probe_2.Place object at 0x038C4F90> <probe_2.Place object at 0x03912AD0>
<probe_2.Place object at 0x038D1790> <probe_2.Place object at 0x03846AB0>
<probe_2.Place object at 0x038DF630>]
[<probe_2.Place object at 0x03920330> <probe_2.Place object at 0x03912C30>
<probe_2.Place object at 0x03912BF0> 0 0 0 0 0 0]
[0 <probe_2.Place object at 0x03912C10>
<probe_2.Place object at 0x03912BD0> 0 0 0 0 0 0]
[0 0 <probe_2.Place object at 0x03912C50> 1 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 <probe_2.Place object at 0x03912B90>
<probe_2.Place object at 0x03912BB0> 0 0]
[0 0 0 0 0 0 1 <probe_2.Place object at 0x03912ED0> 0]
[0 0 0 0 0 0 0 <probe_2.Place object at 0x03912C70>
<probe_2.Place object at 0x03912CB0>]
[0 0 0 0 0 0 0 <probe_2.Place object at 0x03912C90>
<probe_2.Place object at 0x03912CD0>]]
这是错误之前的最后一个,看来一切正常,因此代码似乎正常工作:
[[<probe_2.Place object at 0x036EF6B0> <probe_2.Place object at 0x036D7FF0>
<probe_2.Place object at 0x03660410> <probe_2.Place object at 0x03656AB0>
<probe_2.Place object at 0x036D7470> <probe_2.Place object at 0x036E1AF0>
<probe_2.Place object at 0x03656AB0> <probe_2.Place object at 0x03660410>
<probe_2.Place object at 0x0372F910>]
[<probe_2.Place object at 0x036EF350> <probe_2.Place object at 0x036E1C50>
<probe_2.Place object at 0x036E1C10> <probe_2.Place object at 0x03660670>
<probe_2.Place object at 0x03721D70> <probe_2.Place object at 0x036E1B50>
<probe_2.Place object at 0x036E1B50> <probe_2.Place object at 0x03660470>
<probe_2.Place object at 0x03721D70>]
[<probe_2.Place object at 0x03656A90> <probe_2.Place object at 0x036E1C30>
<probe_2.Place object at 0x036E1BF0> <probe_2.Place object at 0x036E1B90>
<probe_2.Place object at 0x036D7FF0> <probe_2.Place object at 0x03721D70>
<probe_2.Place object at 0x03656AB0> <probe_2.Place object at 0x03656AF0>
<probe_2.Place object at 0x0372F550>]
[<probe_2.Place object at 0x03656A70> <probe_2.Place object at 0x03660410>
<probe_2.Place object at 0x036E1C70> <probe_2.Place object at 0x0372F690>
<probe_2.Place object at 0x0372F910> <probe_2.Place object at 0x036E1B90>
<probe_2.Place object at 0x03656AF0> <probe_2.Place object at 0x036D7FF0>
<probe_2.Place object at 0x03660410>]
[<probe_2.Place object at 0x036EF0F0> <probe_2.Place object at 0x03656AF0>
<probe_2.Place object at 0x03721D70> <probe_2.Place object at 0x036D7470>
<probe_2.Place object at 0x036D7470> <probe_2.Place object at 0x036E1B70>
<probe_2.Place object at 0x03721D70> <probe_2.Place object at 0x03660410>
<probe_2.Place object at 0x036E1B70>]
[<probe_2.Place object at 0x036EF290> <probe_2.Place object at 0x03660670>
<probe_2.Place object at 0x036D7470> <probe_2.Place object at 0x03721D70>
<probe_2.Place object at 0x0372F690> <probe_2.Place object at 0x036E1BB0>
<probe_2.Place object at 0x036E1BD0> <probe_2.Place object at 0x036E1B50>
<probe_2.Place object at 0x03656AF0>]
[<probe_2.Place object at 0x036EF170> <probe_2.Place object at 0x03721D70>
<probe_2.Place object at 0x036E1B50> <probe_2.Place object at 0x036D7470>
<probe_2.Place object at 0x0372F550> <probe_2.Place object at 0x0372F690>
<probe_2.Place object at 0x0372F910> <probe_2.Place object at 0x036E1EF0>
<probe_2.Place object at 0x0372F910>]
[<probe_2.Place object at 0x036EF650> <probe_2.Place object at 0x03656AF0>
<probe_2.Place object at 0x03656AB0> <probe_2.Place object at 0x0372F690>
<probe_2.Place object at 0x03656AF0> <probe_2.Place object at 0x0372F550>
<probe_2.Place object at 0x0372F910> <probe_2.Place object at 0x036E1C90>
<probe_2.Place object at 0x036E1CD0>]
[<probe_2.Place object at 0x036EF130> <probe_2.Place object at 0x03656AB0>
0 0 0 0 0 <probe_2.Place object at 0x036E1CB0>
<probe_2.Place object at 0x036E1CF0>]]
但是即使现在,它仍引发AttributeError: 'int' object has no attribute 'place'
错误AttributeError: 'int' object has no attribute 'place'
。 如上所示,仅需要填充最后一行,但是在上一次迭代中,它的行为就像填充地图的条件还不够,而在前几行中则是如此。
知道为什么会这样吗,我该怎么做才能解决?
try:
places[i,j].place
except AttributeError:
if places[i,j-1].place=='cave':
places[i,j]=Place()
数组中的所有条目都必须是Place
对象吗? 对于第一列之后的列,有时只用Place
填充它们。 如果places[i, j-1].place
不是'cave'
,则永远不会填充places[i, j]
,然后在下一次迭代中, places[i, j-1].place
将引发AttributeError
。 if i>=1 and j>=1:
分支也会发生这种情况。
为什么不在创建时用Place
填充数组,这样就不必继续检查数组中是否包含Place
?
places = numpy.array([[Place() for i in xrange(n)] for j in xrange(n)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.