How to identify which numbers in a list are within each others' range
Arnaud Delobelle
arnodel at googlemail.com
Fri Feb 8 11:06:22 EST 2008
On Feb 8, 1:48 pm, Boris Borcic <bbor... at gmail.com> wrote:
> Arnaud Delobelle wrote:
>
> (...)
>
>
>
> > from itertools import chain
>
> > def overlaps(lst):
> > bounds = chain(*(((x[1],i), (x[2], i)) for i,x in enumerate(lst)))
>
> imho, this is a uselessly painful equivalent of
>
> bounds = ((x[k],i) for i,x in enumerate(lst) for k in (1,2))
>
> Cheers, BB
I did mention that it was awkward (but at the time I just wrote what
came to mind) - thank you for providing a much improved version.
It doesn't deter from the fact that the algorithm is of optimal
complexity (modulo sorting of the list).
--
Arnaud
More information about the Python-list
mailing list