简体   繁体   English

用对象填充一个numpy数组

[英]Fill a numpy array with objects

I have code that sometimes raises an AttributeError . 我的代码有时会引发AttributeError The exact error is : AttributeError: 'int' object has no attribute 'place' I create a numpy array with a=np.eye(9, dtype=object) then fill it with the objects as follows: 确切的错误是: 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()

When I run this code it usually stops at the last if: if places[i,j-1].place=='cave' and places[i-1,j].place=='cave': . 当我运行此代码时,它通常在以下情况下最后停止: if places[i,j-1].place=='cave' and places[i-1,j].place=='cave': But it should work fine, shouldn't it? 但是它应该可以正常工作,不是吗?

I put the print(places) in to check how this process runs and how it is filled. 我放入了print(places)来检查此过程如何运行以及如何填充。 Here is the result for the first run (it has some other object of previous conditions not relevant with the problem, or maybe it is): 这是第一次运行的结果(它具有先前条件的其他一些对象与该问题无关,或者也许与该问题无关):

[[<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>]]

Here is the last one before the error where apparently everything has worked, so the code seems to work fine: 这是错误之前的最后一个,看来一切正常,因此代码似乎正常工作:

[[<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>]]

But even now, it throws the error of AttributeError: 'int' object has no attribute 'place' . 但是即使现在,它仍引发AttributeError: 'int' object has no attribute 'place'错误AttributeError: 'int' object has no attribute 'place' As seen above just the last row needs to be filled but in the previous iteration it behaves as if the conditions to fill the map weren't enough, when in the previous lines they were. 如上所示,仅需要填充最后一行,但是在上一次迭代中,它的行为就像填充地图的条件还不够,而在前几行中则是如此。

Any idea why this is happening, and what can I do to fix it? 知道为什么会这样吗,我该怎么做才能解决?

                try: 
                    places[i,j].place
                except AttributeError:
                    if places[i,j-1].place=='cave':
                        places[i,j]=Place()

Do all entries of your array need to be Place objects? 数组中的所有条目都必须是Place对象吗? For columns after the first, you only sometimes fill them with Place s. 对于第一列之后的列,有时只用Place填充它们。 If places[i, j-1].place wasn't 'cave' , you'll never fill places[i, j] , and then on the next iteration, places[i, j-1].place will raise an AttributeError . 如果places[i, j-1].place不是'cave' ,则永远不会填充places[i, j] ,然后在下一次迭代中, places[i, j-1].place将引发AttributeError This happens on the if i>=1 and j>=1: branch, too. if i>=1 and j>=1:分支也会发生这种情况。

Why not fill the array with Place s at creation time, so you don't have to keep checking whether it has Place s in it? 为什么不在创建时用Place填充数组,这样就不必继续检查数组中是否包含Place

places = numpy.array([[Place() for i in xrange(n)] for j in xrange(n)])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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