[Tutor] Selecting from list
Peter Otten
__peter__ at web.de
Thu Jul 18 21:10:56 CEST 2013
Hs Hs wrote:
> In the following list, is there a simply way to find element less than 200
> sandwiched between two numbers greater than 1000.
>
> a = [3389, 178, 2674, 2586, 13731, 3189, 785, 1038, 25956, 33551]
>
>
> in a, 178 is between 3389 and 2674. How this particular list can be
> selected for further processing. (sorry this is not homework question. I
> want to avoid looping, because I have 300K lines to parse through)
I'm afraid you have to use a loop. Give us your solution, and we'll see if
we can improve it.
As a bonus here's a numpy solution without explicit loops:
>>> a = numpy.array([3389, 178, 2674, 2586, 13731, 3189, 785, 1038, 25956,
33551])
>>> g = a > 1000
>>> l = a < 200
>>> x = g[:-2] & g[2:] & l[1:-1]
>>> numpy.where(x)[0]+1
array([1])
I'll withhold remarks regarding clarity and predictions regarding
performance ;)
More information about the Tutor
mailing list