I have a list of lists with the following values:
field = [[0, 0, 0, -1, 0, 0, 0, 0, 0, 0],
[0, -1, 0, 0, 0, 0, 0, -1, 0, 0],
[0, 0, 0, 0, -1, 0, 0, 0, -1, 0],
[0, 0, -1, -1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, -1, -1]]
I need to find the the nearest value -1 from a given coordinate
I managed to get the index for all matches
My code so far:
x = 0
y = 4
value = -1
coordinates = field[x][y]
for i, row in enumerate(field):
for j, elem in enumerate(row):
if elem == value:
print(i,j)
So for example if the coordinates given are (0,4) it should return the coordinates (1,7) witch matches the nearest value(-1)
Thanks in advance
for j, elem in enumerate(field[x][y:]):
if elem == z:
print(i,j)
return
for i, row in enumerate(field[x+1:]):
for j, elem in enumerate(row):
if elem == z:
print(i,j)
return
field = [[0, 0, 0, -1, 0, 0, 0, 0, 0, 0],
[0, -1, 0, 0, 0, 0, 0, -1, 0, 0],
[0, 0, 0, 0, -1, 0, 0, 0, -1, 0],
[0, 0, -1, -1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, -1, -1]]
x = 2
y = 1
z = -1
def coords(field, x,y,z):
for i, row in enumerate(field[x:]):
for j, elem in enumerate(row):
if elem == z and j > y:
return i+x,j
coords(field,x,y,z)
Output
(2, 4)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.