[英]Python - Iterate over an edge list, for nodes with a specific attribute, find all connected nodes with a different specific attribute?
[英]Searching for a specific string connected to an attribute of an attribute of a list?
我可能用了可怕的措辞,所以这里有一个解释:我有两个类,Tile和Grid。 在图块中,我创建了基本图块格式及其位置,名称和项目。 在Grid中,我使用for循环来正确创建这些图块并添加信息并将其全部存储在“网格”中。我想允许用户在网格内搜索图块具有等于“属性”的属性的图块用户尝试搜索的内容,然后返回所有信息(x和y位置,“名称”和“项目”属性)。
global grid
letters = ["A","B","C","D","E","F","G","H","I"]
class Tile:
def __init__(self, x, y, name=None, items="Empty"):
self.x = x
self.y = y
self.contents = {
"name": name,
"items": items
}
def __repr__(self):
return str(self.contents)
class Grid:
def __init__(self):
self.grid = []
self.resetGrid()
def resetGrid(self):
grid = [[Tile(x, y) for x in range(9)] for y in range(9)]
for n in range(9):
letter = letters[n]
for m in range(9):
grid[n][m].contents = {"name":letter + str(m+1),"items":"Empty"}
return grid
def printGrid(self):
for row in grid:
print()
print("-" * 44)
for tile in row:
name = tile.contents["name"]
#items = tile.contents["items"]
print(name, end=" | ")
print()
print("-" * 44)
def searchGrid(self):
grid = self.resetGrid()
print()
print("")
print("Please select which square you wish to see the information about.")
userinput = input("> ")
for name in tile.contents["name"]:
pring("k")
newgrid = Grid()
newgrid.printGrid()
#newgrid.resetGrid()
谢谢你的帮助。
只需在每个图块中搜索名称,直到找到它即可。 这是你想要的?
def searchGrid(self):
grid = self.resetGrid()
print()
print("")
print("Please select which square you wish to see the information about.")
userinput = input("> ")
for row in self.grid:
for tile in row:
if tile.contents['name'] == userinput:
print(tile.contents)
#other things
break
另请注意拼写错误:
更改pring
到print
在printGrid()
方法printGrid()
grid
更改为self.grid
letters = ["A","B","C","D","E","F","G","H","I"]
class Tile:
def __init__(self, x, y, name=None, items="Empty"):
self.x = x
self.y = y
self.contents = {
"name": name,
"items": items
}
def __repr__(self):
return str(self.contents)
class Grid:
def __init__(self):
self.grid = []
self.resetGrid()
def resetGrid(self):
self.grid = [[Tile(x, y) for x in range(9)] for y in range(9)]
for n in range(9):
letter = letters[n]
for m in range(9):
self.grid[n][m].contents = {"name":letter + str(m+1),"items":"Empty"}
def printGrid(self):
for row in self.grid:
print()
print("-" * 44)
for tile in row:
name = tile.contents["name"]
#items = tile.contents["items"]
print(name, end=" | ")
print()
print("-" * 44)
def searchGrid(self):
print()
print("")
print("Please select which square you wish to see the information about.")
userinput = input("> ")
for n in range(9):
for m in range(9):
if userinput == self.grid[n][m].contents['name']:
print(self.grid[n][m].x)
print(self.grid[n][m].y)
print(self.grid[n][m].contents['name'])
print(self.grid[n][m].contents['items'])
newgrid = Grid()
newgrid.printGrid()
newgrid.searchGrid()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.