用对象填充numpy数组

时间:2014-02-02 21:43:04

标签: python numpy

我的代码有时会引发AttributeError。确切的错误是:AttributeError: 'int' object has no attribute 'place' 我使用numpy创建一个a=np.eye(9, dtype=object)数组,然后按如下方式填充对象:

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'的错误。如上所示,只需要填充最后一行,但在上一次迭代中,它的行为就好像填充地图的条件不够,而在之前的行中它们是。

知道为什么会发生这种情况,我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:1)

                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 s填充数组,这样你就不必继续检查其中是否有Place个?

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