compare range objects
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sat Oct 22 01:32:44 EDT 2011
On Fri, 21 Oct 2011 16:42:16 -0700, SigmundV wrote:
> On Oct 21, 2:55 am, Yingjie Lan <lany... at yahoo.com> wrote:
>>
>> In simulation, one can use range objects to denote a discrete domain,
>> and domain comparison could be very useful. Not just equality, but also
>> things like if one domain is contained in another.
>
> Can't sets [help(set)] be used for this?
Sure. But the downside of sets is that, like lists, they are not lazy,
they actually store every value in them rather than calculate them as
needed, so they are only suitable for relatively small domains. Compare:
>>> sys.getsizeof(range(9999))
20
>>> sys.getsizeof(set(range(9999)))
262256
Now consider:
>>> sys.getsizeof(range(-999999999, 999999999))
20
Converted to a set, the memory requirements will be quite large, and the
processing time to do anything useful with it likewise inflated.
If you need a data type to store large numeric domains, whether discrete
or continuous, a tree of intervals storing the lower and upper bounds is
probably the right solution.
--
Steven
More information about the Python-list
mailing list