from collections.abc import Sequence
class Map(Sequence):
""" Represents a map for a floor as a matrix """
def __init__(self, matrix):
""" Takes a map as a matrix """
self.matrix = matrix
self.height = len(matrix)
self.width = len(matrix[0])
super().__init__()
def __getitem__(self, item):
""" Needed by Sequence """
return self.matrix[item]
def __len__(self):
""" Needed by Sequence """
return len(self.matrix)
def search(self, entity):
""" Returns a generator of tuples that contain the x and y for every element in the map that matches 'entity' """
for row in range(self.height):
for column in range(self.width):
if matrix[row][column] == entity:
yield (row, column)
# Examples
gmap = Map([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]])
for entity in gmap:
print(entity)
我如何能實現__iter__
使Python的迭代矩陣類
for entity in gmap:
print(entity)
產量0 0 0 0 1 0 0 0 0
而不是
[0, 0, 0]
[0, 1, 0]
[0, 0, 0]
這將節省我從需要繼承Sequence
並會使代碼for search()
neater
此外,他們是否還有其他任何我應該使用的魔法方法? (除了__str__
,即時通訊做我得到迭代工作之後)
這是一個非常糟糕的主意。這會讓你的'__iter__'和'__getitem__'彼此不一致。 – user2357112
此外,它實際上不會使任何搜索器「搜索」。 – user2357112