Help with sets

Chris Rebert clp2 at rebertia.com
Tue Oct 5 03:06:53 EDT 2010


On Tue, Oct 5, 2010 at 12:01 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Mon, Oct 4, 2010 at 8:31 PM, B. M. Whealton
> <bwhealton at futurewavedesigns.com> wrote:
>>
>> self._pos = {predicate:{object:set([subject])}}
>>
>>       We have the first dictionary keyed off the first term, the second
>> dictionary keyed off the second term, and the set containing the third
>> terms(note terms plural). I guess I somewhat understand that sets are used
>> to test for membership.  Cannot that be done with dictionaries, though?
>
> You could.  I suspect the pertinent difference here is that dict keys have
> values associated with them.  The same code using a dict as the innermost
> collection would look something like this:
>
> self._pos = {predicate: {object: {subject: None}}}
>
> That's a bit ugly because the None serves no purpose here; the value
> associated with the subject has no meaning in this context.  It also
> uselessly takes up space in memory that could add up to significant wastage
> if the data structure grows to be large.

Indeed, CPython's sets are implemented as something like dictionaries
with dummy values (the keys being the members of the set), with some
optimization(s) that exploit this lack of values.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list