Comparing Dictionaries

Kenneth Love klove at tax.ok.gov
Fri Jul 27 15:24:27 EDT 2007


At 09:55 PM 7/26/2007, Ben Finney wrote:
>Kenneth Love <klove at tax.ok.gov> writes:
>
> > In other words, I consider these two dictionaries to be equivalent:
> >
> >     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'mickey' }
> >     { 'mouse' : 'mickey', 'dog' : 'bone', 'cat' : 'fever' }
> >
> > while these two are not:
> >
> >     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'mickey' }
> >     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'michael' }
> >
> > Any suggestions on how to compare these via some assert statement?
>
>Dictionaries already know how to compare themselves to each other.
>
>     >>> {'dog': 'bone', 'cat': 'fever', 'mouse': 'mickey'} == {'mouse': 
> 'mickey', 'dog': 'bone', 'cat': 'fever'}
>     True
>     >>> {'dog': 'bone', 'cat': 'fever', 'mouse': 'mickey'} == {'mouse': 
> 'michael', 'dog': 'bone', 'cat': 'fever'}
>     False

After verifying this myself, I double checked my code.  It turns out,
the changes I made to get rid of the "import string" from the original
recipe were the problem.  Leaving the parenthesis off lower() and
strip() in the following code causes all sorts of problems:

     config["%s$%s" % (name, key.lower())] = cp.get(sec, key).strip()

That should teach me not to change working code at the same time I am
writing unit tests.  Even so, I realize it won't be the last time I
do something so silly.  Yes, I know about TDD's "write the test first",
but I'm not comfortable with the philosophy of these new fangled unit
tests yet.

Thanks for the help!

Sincerely,
Kenneth Love


-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Kenneth Love                   |         Oklahoma Tax Commission
DP Programmer/Analyst          |         Information Technology
(405) 522 - 5864               |         http://www.tax.ok.gov/
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 





More information about the Python-list mailing list