Re: compare dictionary values



In <dp4124$pki$1@xxxxxxxxxxxxxxxxx>, rbt wrote:

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

Look them up and then compare!? ;-)

> I want to find
> values that have changed. I look for new keys by doing this:
>
> new = [k for k in file_info_cur.iterkeys() if k not in
> file_info_old.iterkeys()]
> if new == []:
> print new, "No new files."
> else:
> print new, "New file(s)!!!"
>
> My key-values pairs are filepaths and their modify times. I want to
> identify files that have been updated or added since the script last ran.

This looks up each `key` from the `new` dictionary and compares the value
with the `old` one. If it's not equal or the key is not present in `old`
the key is appended to the `result`::

def new_and_changed_keys(old, new):
result = list()
for (key, value) in new:
try:
if old[key] != value:
result.append(key)
except KeyError:
result.append(key)
return result

Ciao,
Marc 'BlackJack' Rintsch
.



Relevant Pages

  • Re: dictionary interface
    ... >> I have been searching some more and finally stumbled on this: ... >> lists compare equal. ... >> This seems to imply that the specific method to sort the dictionaries ... >> But that is contradicted by the unittest. ...
    (comp.lang.python)
  • 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)