IntSpan?

Jeff Bienstadt jeffbi at jetsoft.com
Thu Mar 21 12:39:52 EST 2002


"Neal Norwitz" <neal at metaslash.com> wrote in message
news:3C99F049.5A78B149 at metaslash.com...
> Jeff Bienstadt wrote:
> >
> > Does there exist for Python something akin to the Set::IntSpan module
for
> > Perl?
> >
> > I'm looking for something that inteligently handles and manipulates
spans
> > of integers, such as:
> >    2-356,456,458-500
>
> I'm not familiar with IntSpan, but have you looked at the builtin range()?
> >>> print range.__doc__
> range([start,] stop[, step]) -> list of integers
>
> Return a list containing an arithmetic progression of integers.
> range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
> When step is given, it specifies the increment (or decrement).
> For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
> These are exactly the valid indices for a list of 4 elements.
>
>
> range(2, 356)
> range(458, 500)
>
> or if you want the list above to be all together:
>
> range(2, 356) + [456] + range(458, 500)
>
> There aren't sets in python, but you can use a dictionary to emulate
> sets.  To create an intset with a dictionary:
>
> intset = {}
> for i in range(2, 356) + [456] + range(458, 500):
> intset[i] = 1
>
> print intset.has_key(55), intset.has_key(457)
> print intset.keys()
>
> I'm sure there are set implementations on ASPN.
>
> HTH,
> Neal

Thanks for your reply.

Yes, I knew about range, but that's not quite what I'm looking for.

Using range to create the full list of integers seems prohibitively
expensive --- spans in the range 1-400000 are not at all out of line.

I'm envisioning a class that stores (for example) a list of tuples and/or
integers
to express each portion of the span, such as
    [(2,356), 456, (458,500)]

Then, the class might be asked to insert the value 457 into the
span, which would then result in a "merging" of the items in the
internal list, such as:
    [(2,356), (456, 500)]

Other operations (removing a value or a sub-span, merging another
span, etc.) would be useful as well.

I can see from your reply how range could be useful in the
implementation, for creating temporary lists of values to work
with but again, since the spans could be quite long, this
seems expensive.

I'm sure that I can implement something like this myself, but
I was hoping that such a thing already existed, because a) I would
prefer to not have to re-invent the wheel, and b) existing code
would almost certainly be better that anything I could write given
my current (week-old!) knowledge of Python.

(Also, the fact that the Perl module I mentioned uses Set is not
really important, it just came up while I was searching --- further
searching also turned up an Array::IntSpan Perl module.  I'm not
a Perl guy at all, and attempting to translate code from a language
that I don't know at all into one that I am just learning strikes me
as painful :-> )

If I can't find anything that already does what I'm looking for, I'll go
ahead and tackle it myself.

Again, thanks for your reply.

---jkb






More information about the Python-list mailing list