Re: dictionary interface



Op 2005-10-05, Steve Holden schreef <steve@xxxxxxxxxxxxx>:
> Antoon Pardon wrote:
>>
>> I have been searching some more and finally stumbled on this:
>>
>> http://docs.python.org/ref/comparisons.html
>>
>> Mappings (dictionaries) compare equal if and only if their sorted
>> (key, value) lists compare equal. Outcomes other than equality are
>> resolved consistently, but are not otherwise defined.
>>
>> This seems to imply that the specific method to sort the dictionaries
>> is unimported (as long as it is a total ordering). So I can use whatever
>> method I want as long as it is achieves this.
>>
>> But that is contradicted by the unittest. If you have a unittest for
>> comparing dictionaries, that means comparing dictionaries has a
>> testable characteristic and thus is further defined.
>>
>> So I don't need a full implementation of dictionary comparison,
>> I need to know in how far such a comparison is defined and
>> what I can choose.
>>
> The dict unit tests are probably trying to ensure that the dictionary
> ordering doesn't change from version to version, which is probably a
> good idea in case someone (foolishly?) deciess to rely on it.

I doubt that. Just to check I tried the following:

class Tree:

def __lt__(self, term):
return len(self) < len(term)

And the test passed.

> I can't help wondering, though, under what conditions it actually makes
> sense to compare two dictionaries for anything other than equality.

Yes that is part of the problem, because I can't think of such a
condition it is hard to think of what extra constraints could be
usefull here.

Anyway, I have searched the source of the test for all testing
with regards to < and after some browsing back and fore it seems
it all boils down to the following two tests.

self.assert_(not {} < {})
self.assert_(not {1: 2} < {1L: 2L})

--
Antoon Pardon
.



Relevant Pages

  • Re: dictionary interface
    ... which constitutes a total ordering over all dicts (so you can ... >> natural total ordering, it is ever so slightly arbitrary. ... > lists compare equal. ... >This seems to imply that the specific method to sort the dictionaries ...
    (comp.lang.python)
  • Fast Data Comparison (dict v. list. v string)
    ... many comparisons to fixed-size lists of fixed-length strings. ... my implementation uses dictionaries to store each string. ... all I have to do to compare the two ... it appears that strings are constant as I can't assign individual ...
    (comp.lang.python)
  • Re: Fast Data Comparison (dict v. list. v string)
    ... > I'm a relative novice at Python and am working on some optimizations in my ... my implementation uses dictionaries to store each string. ... all I have to do to compare the two ...
    (comp.lang.python)
  • Re: Recommend a PPC Engligh Dictionary/Thesaurus
    ... not let me select the words I want to check to compare with other ... dictionaries. ... with Thesaurus? ... Prev by Date: ...
    (microsoft.public.pocketpc)
  • Re: Similarity searching
    ... a factor of N*log, if the arrays are big you won't in general have to compare to the last element so I don't see the factor of L**2, and I don't see where that C**2 factor is coming from at all. ... There are also sorting algorithms which don't compare each item to every other item, yet still generate a sorted result. ... Similarity searching is still an active science, but there are a lot of useful algorithms out there now. ...
    (comp.lang.fortran)