IntSpan?

Neal Norwitz neal at metaslash.com
Thu Mar 21 09:38:01 EST 2002


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



More information about the Python-list mailing list