I need to search for an item in a list around a given index with in a given radius. Currently I use this function to generate alternating offsets for the search:
def generateSearchIndizes(radius):
for i in range(1, radius + 1):
yield i
yield -i
The code that does the search looks something like this:
for i in generateSearchIndizes():
if pred(myList[baseIndex + i]):
result = myList[baseIndex + i]
break # terminate search when first item is found
My question is, is there a more elegant way to generate the search indizes, maybe without defining a special function?
I don’t think there’s a more elegant way. Your code is very simple and clear.
Yes, that’s definitely possible.