473,738 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.i terkeys() if k not in
file_info_old.i terkeys()]
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 8731
In <dp**********@s olaris.cc.vt.ed u>, 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.i terkeys() if k not in
file_info_old.i terkeys()]
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(k ey)
except KeyError:
result.append(k ey)
return result

Ciao,
Marc 'BlackJack' Rintsch
Dec 30 '05 #2
rbt
Marc 'BlackJack' Rintsch wrote:
In <dp**********@s olaris.cc.vt.ed u>, 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.i terkeys() if k not in
file_info_old.i terkeys()]
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(k ey)
except KeyError:
result.append(k ey)
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
2518
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 want to # init a class from that dictionary. # However, I don't want to specify the # dictionary gets by hand # since it is error prone.
125
7200
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
7771
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. It seems the HashCode provider is lost or not needed in the Generic Dictionary anymore so wondering if this is how you do the same: Dictionary<string, Node> nodes = new Dictionary<string, Node>( new StringCompInsensitive() ); ....
5
47265
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 there a better way? MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); Dictionary<string, string> dictionary = new Dictionary<string, string>(); dictionary.Add("name", "andrew"); dictionary.Add("home",...
14
3465
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 list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the dictionary keys and values to be sets. Presently {1:0,2:0,3:0}.keys() will produce but it could also...
11
14467
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 dozen or so of records) and you want to build a list of differences about these two sets. I like to end up with 3 lists: what's in A and not in B, what's in B and not in A, and of course, what's in both A and B.
3
2512
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 sqlite3 database table. Here's the context: The OMR card reader sends a stream of 69 bytes over the serial line; the last byte is a carriage return ('\r') indicating the end of record. Three pairs (in specific positions at the beginning of the...
4
34160
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 foreach statement when the first item is removed from the Dictionary. From looking at Dictionary's methods, I couldn't find anything to create a copy of the Values before starting the foreach loop. Help? static void someTest() {
0
5084
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 first matching value only. Mine gets one arbitrary matching value, determined by the algorithm used for constructing the search dict (not specified in my solution). What the OP actually said was that he can't throw away the case. You assume...
0
8787
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9334
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.