Help with sets

Ian Kelly ian.g.kelly at gmail.com
Tue Oct 5 03:01:00 EDT 2010


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.

The other major difference between dicts and sets is that sets provide
mathematical operations such as intersection and difference that dicts do
not, but that doesn't appear to be relevant in this context.


>        Also, if you ran this for loop, below, what is being yielded, the
> key or the value in the dictionary?  I refer to this for loop here:
> for subject in self._pos[predicate][ojbect]: yield (subject, predicate,
> object)
>

Why don't you try it and see what comes out?

The expression "self._pos[predicate][object]" refers to the set of subjects
associated with predicate and object.  Looping over it is just iterating
through each subject in that set.

Cheers,
Ian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101005/2482cff2/attachment-0001.html>


More information about the Python-list mailing list