Overlap in python

Ann anna.konstorum at gmail.com
Tue Aug 4 14:46:14 EDT 2009


On Aug 4, 11:31 am, Marcus Wanner <marc... at cox.net> wrote:
> On Aug 4, 2: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
>
> Just take all the values, put them in a list, and use min() and max().
> For example:
>
> import string
>
> def makelist(values):
>     values = string.replace(values, ' ', '')
>     listofvaluepairs = string.split(values, ',')
>     listofvalues = []
>     for pair in listofvaluepairs:
>         twovalues = string.split(pair, '-')
>         listofvalues.append(int(twovalues[0]))
>         listofvalues.append(int(twovalues[1]))
>     return listofvalues
>
> values = '5-9, 7-10, 3-6'
> values = makelist(values)
> print('Values: %d-%d' %(min(values), max(values)) )
>
> Marcus

Thank you for your help, this is a very nice program but it does not
address the issue that I have values that do not overlap, for example
'c' and 'd' do not overlap in the above example and thus I would not
want to output 3-20 but the individual positions instead.  In
addition, my inputs are not ordered from smallest to largest, thus I
could have a situation where a=5-9, b=15-20, c=7-10, etc., and I would
still want an output of: a.c = 5-10, b=15-20.  I apologize for the
complication.

Thank you again!

Jay



More information about the Python-list mailing list