Why aren't OrderedDicts comparable with < etc?

Piet van Oostrum piet at cs.uu.nl
Sat Jul 25 14:18:46 EDT 2009


>>>>> Albert van der Horst <albert at spenarnc.xs4all.nl> (AvdH) wrote:

>AvdH> With regard to < and > you are right.
>AvdH> But I think there is a sensible == w.r.t. dict's.
>AvdH> It is to mean that for each key   dict1(key) == dict2(key)
>AvdH> (implying that their key set must be the same)

>AvdH> [I could have used that for one of the euler problems.
>AvdH> You have a 4 by 4 field containing a red or blue square.
>AvdH> That is naturally a mapping of (1,1) ..(4,4) tuples to one
>AvdH> of the objects `blue' `red'. After moving a square you
>AvdH> want to know whether this is a map you already have encountered.]

So what's the problem?

piet$ python3
Python 3.1 (r31:73578, Jun 27 2009, 21:49:46) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import OrderedDict
>>> a = OrderedDict()
>>> b = OrderedDict()
>>> a[1]=2
>>> b[1]=2
>>> a[3]=4
>>> b[3]=4
>>> a==b
True
>>> b[5]=6
>>> a==b
False
>>> d1 = dict((str(i), i) for i in range (10))
>>> d2 = dict((str(i), i) for i in range (10))
>>> d1 == d2
True

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list