[英]How to search for a list in a list of lists
如何具体搜索列表列表中的特定列表:
假设我有一个列表grid_list,其中包含许多其他列表,我想通过grid_list搜索以找到其中一个具体我怎么能这样做? 这是我认为应该工作的代码,但不是:
import sys #sys has been imported to take in user input
import random #random has been imported to generate random numbers to store the ants food in in the grid
#grid list is a generated list used to store grid values
grid_list = []
grid_size=0 #grid size is a variable that is assigned a value in function space and used in other functions, has default value of zero
ant_position="" #ants current position in grid used in more than one function
food_location="" #foods position in grid used in more than one function
#function for setting up a grid
def space():
print("please enter the size of the square grid you want")
#grid size is assigned by user input
global grid_size
grid_size = int(sys.stdin.readline())
#embedded for loops to set up a list that stores grid points
for count in range(0,grid_size):
x_number= count+1 #x number is the x number of the cell in the grid
for count in range(0,grid_size):
y_number= count+1 # y number is the y number of the cell in the grid
grid_list.append([x_number,y_number,0,0,0]) #this stores a list within a list, so the first two values of the second list represent the co-ordinates of the cell the last three represent pheromone, food location, ant location (both food and ant location take value of one if either are in there)
def locations():
global food_location
food_location_x = (random.randint(1,grid_size)) #gives the x value of the cell the food is stored in
food_location_y = (random.randint(1,grid_size)) #gives the y value of the cell the food is stored in
food_location = [food_location_x,food_location_y,0,0,0] #gives the full location of the food and will use this to in other functions to reference the grid list
for count in range(0,4):
grid_list[[grid_list.index([food_location])][count]] = [food_location_x,food_location_y,0,1,0]
print(grid_list[grid_list.index(food_location)])
此代码设置一个包含许多其他列表的列表(空间函数),然后更改列表中的一个列表(grid_list)以表示它包含食物。
然而,而不是这个工作,我得到一个值错误,说[2,1,0,0,0]不在列表中或代码给出的任何其他网格值(在这种情况下,2和1可以被任何正整数替换)。 有人可以向我解释我如何更改它,以便正确搜索它?
我可以看到一个错误:
grid_list.index([food_location])
您将food_location
放入列表中。 但food_location
已经是一个列表,所以现在列表中有一个列表。 当你执行grid_list.index(that)
,它会告诉你grid_list
没有双重嵌套列表。
老实说,看起来你正在做一个非常时髦的方法来获得grid_list
的随机抽样。 您应该使用random.choice
或random.sample
分别从集合中获取一个或多个随机元素。
这段代码中有两个错误。 第一个,先前由roippi指出,是你在搜索之前将food_location
放在一个列表中。 第二个是你做了四次相同的事情。 您正在搜索[x,y,0,0,0]
,替换它,然后在列表中搜索相同的[x,y,0,0,0]
。 由于您刚刚替换了该元素,因此它不再存在,您将收到您所看到的错误。
正如罗比指出的那样,更好的方法是获得4个替换位置
random.sample(grid_list, 4)
并修改返回的四个列表中的每一个,以便第四个元素为1. random.sample将返回grid_list中的相同列表,因此修改这些列表的元素也将在grid_list中修改它们。
您将二维数组表示为列表列表,并且您正在尝试搜索它以查找项目。 您根本无法使用grid_list.index()
进行此类搜索。 grid_list
中的项目是网格的列,因此grid_list.index(item)
将项目与整个列进行比较,因此找不到匹配项。
我建议定义一个可以进行所需搜索的函数。 就像是:
def find_in_grid(grid, item_to_find):
"""search for a given item in a grid
If found, the indexes (x, y) are returned. Otherwise, returns None.
"""
for row in range(len(grid)):
for column in range(len(grid[row])):
if grid[row][column] == item_to_find:
return (row, column)
return None # item not found in the grid
当然,定义一个简单的Grid
类并给它一个find()
或index()
方法会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.