Reputation: 625
I have a custom class that I have made. I make a list out of that class:
grid = []
def make_grid(r,c):
global grid
grid = [grid_object(x,y) for x in range(r) for y in range(c)]#Thanks Adam
make_grid(row, columns) #this makes the grid
class grid_object(object):#Thanks Adam
def __init__(self, x, y):
self.x, self.y = x, y
self.item = "Blank"
self.tag = "Nothing"
I want to hold a couple of grid's indexs for a side quest. Below I only showed one.
baby_grid = ''
def baby_side_quest():
global grid
global baby_grid
baby_grid = [i for i, j in grid if j.tag == "Baby_Bear"]
print baby_grid
I can get the baby_grid as a list. Here the code just prints:
>>>[2]
But what I really want is just:
>>> 2
How can I do that without having to write baby_grid[0] everywhere?
I just added this little function.
def get_int_from_list(list_thing):
return list_thing[0]
I just wonder if there is a way that I don't know of that would make my code really concise. If you have a better way of doing this I'd love you see that code.
Upvotes: 0
Views: 113
Reputation: 2379
I don't know how pythonic it is considered, but you could do that the old fashioned way:
for i, j in grid:
if j.tag == "Baby_Bear":
return i
Upvotes: 1
Reputation: 1220
Will your baby_grid
always only consist of one item? If so, you could simply do
baby_grid = [i for i, j in grid if j.tag == "Baby_Bear"][0]
Upvotes: 1