473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

compare dictionary values

rbt
What's a good way to compare values in dictionaries? 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.

Thanks,
rbt
Dec 30 '05 #1
2 8716
In <dp**********@solaris.cc.vt.edu>, 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
Dec 30 '05 #2
rbt
Marc 'BlackJack' Rintsch wrote:
In <dp**********@solaris.cc.vt.edu>, 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


Thanks Marc! I changed this line:

for (key, value) in new:

To this:

for (key, value) in new.iteritems():

And, it works great. Thanks again.
Dec 30 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: brianobush | last post by:
# # My problem is that I want to create a # class, but the variables aren't known # all at once. So, I use a dictionary to # store the values in temporarily. # Then when I have a complete set, I...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
0
by: William Stacey [MVP] | last post by:
Trying to figure out Dictionary<> and using CaseInsensitive Comparer<> like I did with normal Hashtable. The Hashtable can take a case insenstive Comparer and a Case insensitive HashCode provider....
5
by: Andrew Robinson | last post by:
I need to serialize a dictionary so I can encode the contents. I have the following working but the size seems large. I am guessing that I am serializing the entire object not just the data. Is...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
11
by: John Henry | last post by:
Hi list, I am sure there are many ways of doing comparision but I like to see what you would do if you have 2 dictionary sets (containing lots of data - like 20000 keys and each key contains a...
3
by: Rich Shepard | last post by:
I need to learn how to process a byte stream from a form reader where each pair of bytes has meaning according to lookup dictionaries, then use the values to build an array of rows inserted into a...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
0
by: J. Cliff Dyer | last post by:
Please keep the discussion on-list. On Fri, 2008-09-05 at 15:36 +0200, Maric Michaud wrote: That does not get the same behavior as the OP's original solution. The OP's solution retrieves the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.