Overlap in python

Albert van der Horst albert at spenarnc.xs4all.nl
Wed Aug 5 12:35:18 CEST 2009

In article <bf29ddcb-347f-49ca-a1ef-66cd5fae4e02 at z4g2000prh.googlegroups.com>,
Jay Bird  <jay.bird0804 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?

That is an algorithmic question and has little to do with Python.
You could proceed as follows:
-  Order your data by the lower limit
-  Combine everything with the same lower limit.
-  Then combine every pair of consecutive entries that overlap.

(In you case the second step is not needed.)

>Thank you very much!

Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

More information about the Python-list mailing list