Overlap in python

DuaneKaufman duane.kaufman at gmail.com
Tue Aug 4 16:32:32 EDT 2009


On Aug 4, 1:15 pm, Jay Bird <jay.bird0... at gmail.com> wrote:
> Hi everyone,
>
> I've been trying to figure out a simple algorithm on how to combine a
> list of parts that have 1D locations that overlap into a non-
> overlapping list.  For example, here would be my input:
>
> part name   location
> a                  5-9
> b                  7-10
> c                  3-6
> d                  15-20
> e                  18-23
>
> And here is what I need for an output:
> part name   location
> c.a.b            3-10
> d.e               15-23
>
> I've tried various methods, which all fail.  Does anyone have an idea
> how to do this?
>
> Thank you very much!
> Jay

Hi,

You have gotten some excellent feedback (all without installing
anything extra to your Python install), but might I suggest the use of
an interval arithmetic package to aid in this?

For instance the interval module found at: http://members.cox.net/apoco/interval/
can be put to use:

Given your data above:
> part name   location
> a                  5-9
> b                  7-10
> c                  3-6

from interval import Interval
>>> a_int=Interval(5,9)
>>> b_int=Interval(7,10)
>>> c_int=Interval(3,6)
>>> a_int.overlaps(b_int)
True
>>> a_int.overlaps(c_int)
True
>>> b_int.overlaps(c_int)
False

Duane



More information about the Python-list mailing list