Re: compare dictionary values
- From: Marc 'BlackJack' Rintsch <bj_666@xxxxxxx>
- Date: Fri, 30 Dec 2005 22:06:28 +0100
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
.
- Follow-Ups:
- Re: compare dictionary values
- From: rbt
- Re: compare dictionary values
- References:
- compare dictionary values
- From: rbt
- compare dictionary values
- Prev by Date: Re: python coding contest
- Next by Date: Re: python coding contest
- Previous by thread: compare dictionary values
- Next by thread: Re: compare dictionary values
- Index(es):
Relevant Pages
|