compare dictionary values

Tim Williams (gmail) tdwdotnet at gmail.com
Fri Dec 30 16:58:49 EST 2005


In <dp4124$pki$1 at solaris.cc.vt.edu>, rbt wrote:

>
> > What's a good way to compare values in dictionaries?


Do you need to compare dictionaries, if its an option it would be
simpler/cheaper  to compare  each entry from your file listing with entries
in a single dict and act accordingly, mainly because you will already have
built your path/date pairs from your dir listing,  adding them to a dict
purely for comparison is an extra step.

import os

new_files = [ ]
changed = [ ]

listing = os.listdir('c:/python24')
for a_file in listing:
    f_date = os.path.getmtime(os.path.join('c:/python24' , a_file))
    try
        if mydict[a_file] != f_date:
            changed.append(a_file)
    except:
        new_files.append(a_file)
    mydict[a_file] = f_date

deleted = [ key for key in mydict if not key in listing ]

for x in deleted:
    del mydict[x]

print changed
print new_files
print deleted

HTH :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051230/2a79ca1c/attachment.html>


More information about the Python-list mailing list