[Tutor] Selecting from list
Jim Mooney
cybervigilante at gmail.com
Sat Jul 20 03:40:26 CEST 2013
> a = [0,3389, 178, 2674, 199, 2586, 13731, 12, 13, 14, 3189, 15, 785, 10, 1038,
> 25956, 6, 8, 2, 3551, 2]
>
> meets_requirements = [(a[idx-1],a[idx],a[idx+1]) for idx in range(1,len(a)-1) \
> if a[idx] < 200 and a[idx-1] > 1000 and a[idx+1] > 1000]
>
> # result [(3389, 178, 2674), (2674, 199, 2586)]
Although on third thought, a real loop is on average fastest if the
requirement is you only need find the first triplet, or there is only
one, since you can then break out of the loop. I don't think you can
break out of a comprehension. If you can it would be an ugly business,
IMHO ;')
> --
> Jim
>
> The universe is made up of patches that are only woven together as
> they are accessed. Sometimes a stitch is dropped and something really
> weird happens. Only nobody will ever believe you
--
Jim
The universe is made up of patches that are only woven together as
they are accessed. Sometimes a stitch is dropped and something really
weird happens. Only nobody will ever believe you
More information about the Tutor
mailing list