按实例属性迭代 class 的实例
Iterating through instances of a class by instance attribute
我有一个名为 House 的 class,我想制作一些实例,这些实例将在视觉上排列在网格中:
class House(object):
def __init__(self, row, col):
self.row = row
self.col = col
self.rooms = 0
我想按行和列遍历此网格中的所有 House 实例:
for row in range(GRIDHEIGHT):
for col in range(GRIDWIDTH):
thisHouse = House()
thisHouse.row = row
thisHouse.col = col
稍后在代码中我想遍历已创建的实例...
for row in range(GRIDHEIGHT):
for col in range(GRIDWIDTH):
??? something something ???
if thisHouse.room =< 1:
do stuff
else:
thisHouse.room = +1
etc
“???什么什么???”是我不确定如何继续的地方,必须以某种方式搜索 thisHouse 的实例(我应该认为 thisHouse.row 和 thisHouse.col)直到与 GRIDHEIGHT 和 GRIDWIDTH 中的行和列匹配,希望说得通。我不确定该怎么做,到目前为止的研究表明 _dir() 可能有用,但似乎显示属性而不是属性的内容。
也许“???什么什么???”应该是...
for thisHouse.row = row and thisHouse.col = col:
但我认为这不会奏效。我正在大修这里的一大块代码,所以此时不能只测试这一点,所以建议表示赞赏。
你为什么不翻转问题?您在 House 上存储网格信息,而不是在网格上存储 house/objects 信息。
你可以有一个代表网格的二维数组,然后在每个单元格中,你都有你想要的信息,也就是那个单元格中的对象,如果有一个通用的对象集合或像你有房子、宠物、汽车标签的字典。
一些伪代码:
for x in grid:
for cell in x:
#here you have the cell:
for obj in cell.objs:
if (obj is House):
do stuff
当然,如果您对自己想要做什么以及那里有什么元素有更多的了解,您可以优化您在单元格内的搜索,例如:
for x in grid:
for cell in x:
#here you have the cell:
for h in cell.objs["houses"]:
do stuff
当然你还可以在房子里保存坐标信息class。
以这种方式放置您的数据将允许轻松地遍历房屋,即使分布不均匀,它也将允许进行诸如查找邻居等操作
IIUC,你想要实现的是:
import itertools
class House(object):
def __init__(self, row, col):
self._row = row
self._col = col
GRIDHEIGHT = 10
GRIDWIDTH = 10
houses = {(col, row): House(row, col) for col, row in
itertools.product(range(GRIDWIDTH), range(GRIDHEIGHT))}
或者你也可以使用双嵌套字典:
houses = {col: {row: House(row, col) for row in range(GRIDHEIGHT)} for col in range(GRIDWIDTH)}
将通过 houses[(col, row)]
或 houses[col][row]
:
访问相关房屋
for col in range(GRIDWIDTH):
for row in range(GRIDHEIGHT):
houses[col][row].foo() # or houses[(col, row)].foo()
我有一个名为 House 的 class,我想制作一些实例,这些实例将在视觉上排列在网格中:
class House(object):
def __init__(self, row, col):
self.row = row
self.col = col
self.rooms = 0
我想按行和列遍历此网格中的所有 House 实例:
for row in range(GRIDHEIGHT):
for col in range(GRIDWIDTH):
thisHouse = House()
thisHouse.row = row
thisHouse.col = col
稍后在代码中我想遍历已创建的实例...
for row in range(GRIDHEIGHT):
for col in range(GRIDWIDTH):
??? something something ???
if thisHouse.room =< 1:
do stuff
else:
thisHouse.room = +1
etc
“???什么什么???”是我不确定如何继续的地方,必须以某种方式搜索 thisHouse 的实例(我应该认为 thisHouse.row 和 thisHouse.col)直到与 GRIDHEIGHT 和 GRIDWIDTH 中的行和列匹配,希望说得通。我不确定该怎么做,到目前为止的研究表明 _dir() 可能有用,但似乎显示属性而不是属性的内容。
也许“???什么什么???”应该是...
for thisHouse.row = row and thisHouse.col = col:
但我认为这不会奏效。我正在大修这里的一大块代码,所以此时不能只测试这一点,所以建议表示赞赏。
你为什么不翻转问题?您在 House 上存储网格信息,而不是在网格上存储 house/objects 信息。 你可以有一个代表网格的二维数组,然后在每个单元格中,你都有你想要的信息,也就是那个单元格中的对象,如果有一个通用的对象集合或像你有房子、宠物、汽车标签的字典。
一些伪代码:
for x in grid:
for cell in x:
#here you have the cell:
for obj in cell.objs:
if (obj is House):
do stuff
当然,如果您对自己想要做什么以及那里有什么元素有更多的了解,您可以优化您在单元格内的搜索,例如:
for x in grid:
for cell in x:
#here you have the cell:
for h in cell.objs["houses"]:
do stuff
当然你还可以在房子里保存坐标信息class。 以这种方式放置您的数据将允许轻松地遍历房屋,即使分布不均匀,它也将允许进行诸如查找邻居等操作
IIUC,你想要实现的是:
import itertools
class House(object):
def __init__(self, row, col):
self._row = row
self._col = col
GRIDHEIGHT = 10
GRIDWIDTH = 10
houses = {(col, row): House(row, col) for col, row in
itertools.product(range(GRIDWIDTH), range(GRIDHEIGHT))}
或者你也可以使用双嵌套字典:
houses = {col: {row: House(row, col) for row in range(GRIDHEIGHT)} for col in range(GRIDWIDTH)}
将通过 houses[(col, row)]
或 houses[col][row]
:
for col in range(GRIDWIDTH):
for row in range(GRIDHEIGHT):
houses[col][row].foo() # or houses[(col, row)].foo()