0

I would like to find the first integer after a given integer that is not in a list of integers, nanIndices. I try

next(x for not (x in nanIndices))

and

next(x for x >0 and x not in nanIndices)

and

 next(x for x not in nanIndices)

and get

*** SyntaxError: invalid syntax

in both cases. I then try

 next(x for x in range(1,1000) and x not in nanIndices)

and get

*** NameError: name 'x' is not defined

What I particularly don't understand is that

next(x for x in nanIndices)

works fine but

next(x for x not in nanIndices)

gives a syntax error

The suggested solution does not work because it returns an array, not the next integer to satisfy the condition.

OtagoHarbour
  • 3,969
  • 6
  • 43
  • 81
  • The syntax for a condition in a comprehension is `if`, not `and`. The `for x in range(1, 1000)` part is not a condition so it does not make sense to write `and` here to add another condition. See e.g. [here](https://stackoverflow.com/questions/24442091/list-comprehension-with-condition). – kaya3 Oct 18 '22 at 20:05
  • Is `nanIndices` sorted? If so, you can use [`bisect.bisect()`](https://docs.python.org/3/library/bisect.html#bisect.bisect) to find where to start looking. – 001 Oct 18 '22 at 20:40
  • Thanks, @JohnnyMopp. It is ordered and >= 0. So bisect.bisect_left(nanIndices, -1) works for me. – OtagoHarbour Oct 18 '22 at 21:31

0 Answers0