473,602 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comparing dictionaries

I want to compare two dicts that should have identical info just in a
different data structure. The first dict's contents look like this. It
is authoritative.. . I know for sure it has the correct key value pairs:

{'001' : '01'}

The second dict's contents are like this with a tuple instead of a
string for the key:

{('This is one', '001'): '01'}

Pseudo Code:
for key, value in first_dict.iter items():
# How do I do the following line?
if key not in second_dict or if it is, but has has the wrong value,
then let me know

Jun 27 '08 #1
5 2076
On May 7, 4:08 pm, brad <byte8b...@gmai l.comwrote:
I want to compare two dicts that should have identical info just in a
different data structure. The first dict's contents look like this. It
is authoritative.. . I know for sure it has the correct key value pairs:

{'001' : '01'}

The second dict's contents are like this with a tuple instead of a
string for the key:

{('This is one', '001'): '01'}

Pseudo Code:
for key, value in first_dict.iter items():
# How do I do the following line?
if key not in second_dict or if it is, but has has the wrong value,
then let me know
Well

for k, v in first_dict.item s():
if k not in second_dict or k in second_dict and k[v] !=
second_dict[k]:
let you know?
Jun 27 '08 #2
brad wrote:
I want to compare two dicts that should have identical info just in a
different data structure. The first dict's contents look like this. It
is authoritative.. . I know for sure it has the correct key value pairs:

{'001' : '01'}

The second dict's contents are like this with a tuple instead of a
string for the key:

{('This is one', '001'): '01'}
It looks like extracting key[1] from each key of the second dictionary
should yield the keys of the first dictionary. If that is the case, then
the following should do it:

d1 = {'001':'01', '002':'02'}
d2 = {('This is one', '001'): '01', ('This is two', '002'): '02'}

if d1 == dict( zip( (k[1] for k in d2.keys()), d2.values() ) ):
print "They're 'equal'."

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 27 '08 #3
brad <by*******@gmai l.comwrites:
I want to compare two dicts that should have identical info just in a
different data structure. The first dict's contents look like this. It
is authoritative.. . I know for sure it has the correct key value
pairs:

{'001' : '01'}
-refdict
>
The second dict's contents are like this with a tuple instead of a
string for the key:

{('This is one', '001'): '01'}
-multidict
>
Pseudo Code:
for key, value in first_dict.iter items():
# How do I do the following line?
if key not in second_dict or if it is, but has has the wrong value,
then let me know
I think it's best to iterate over items of the second dictionary
first. I am assuming that a key can be repeated in the second
dictionary, otherwise it could be a bit simpler:

missing_keys = set(first_dict)
for keys, val in second_dict.ite ritems():
for key in keys:
missing_keys.di scard(key)
if first_dict.get( key, val) != val:
print "wrong value for", key, 'in', keys
if missing_keys:
print 'some keys are missing:', ',',join(remain ing)

--
Arnaud
Jun 27 '08 #4
Hello,
I want to compare two dicts that should have identical info just in a
different data structure. The first dict's contents look like this. It
is authoritative.. . I know for sure it has the correct key value pairs:

{'001' : '01'}

The second dict's contents are like this with a tuple instead of a
string for the key:

{('This is one', '001'): '01'}

Pseudo Code:
for key, value in first_dict.iter items():
* *# How do I do the following line?
* *if key not in second_dict or if it is, but has has the wrong value,
then let me know
def cmp_dicts(first , second):
return sorted(first.it eritems()) == \
sorted(((k[1], v) for k, v in second.iteritem s()))

Google for DSU (Decorate-Sort-Undecorate)

HTH,
--
Miki <mi*********@gm ail.com>
http://pythonwise.blogspot.com
Jun 27 '08 #5

"Miki" <mi*********@gm ail.comwrote in message
news:ad******** *************** ***********@b9g 2000prh.googleg roups.com...
Hello,
I want to compare two dicts that should have identical info just in a
different data structure. The first dict's contents look like this. It
is authoritative.. . I know for sure it has the correct key value pairs:

{'001' : '01'}

The second dict's contents are like this with a tuple instead of a
string for the key:

{('This is one', '001'): '01'}

Pseudo Code:
for key, value in first_dict.iter items():
# How do I do the following line?
if key not in second_dict or if it is, but has has the wrong value,
then let me know
Does this work for you?

for key,value in second_dict.ite ritems():
try:
if first_dict[key(1)] == value:
return True
else:
return False:
except KeyError:
return False

tjr

Jun 27 '08 #6

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

Similar topics

14
8646
by: Kill Bill | last post by:
type(i) == "<type 'float'>" this always returns false. How come? type(i)returns <type 'float'> if i is a float so why isn't == working?
0
1359
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are fairly simple (searching/organizing large boardgame databases) but use a lot of memory (1-6GB). The memory is mostly used for simple structures like trees or relations. Typically there will be a few large dictionaries and many small...
8
2613
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
3
2364
by: Shivram U | last post by:
Hi, I want to store dictionaries on disk. I had a look at a few modules like bsddb, shelve etc. However would it be possible for me to do the following hash = where the key is an int and not a string bsddb requires that both the key,value are string. shelve does support values being object but not the keys. Is there any
210
10389
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again I am missing that feature. Maybe there is something wrong with my programming style, but I rather think it is generally useful. I fully agree with the following posting where somebody complains why so very basic and useful things are not part...
1
1483
by: François Pinard | last post by:
Hi, people. I noticed today that dictionaries seem to support `==' comparison. (Retrospectively, it is strange that I never needed it before! :-) Yet, before relying on this, I seeked for confirmation in the Python manuals, and did not succeed in finding it. In particular: http://www.python.org/doc/2.3.5/lib/typesmapping.html is silent on the subject. As for:
19
3822
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that handles GetHashCode and Equal? If so, is such a wrapper present in the standard class library?
8
1742
by: placid | last post by:
Hi all, Just wondering if anyone knows how to pop up the dialog that windows pops up when copying/moving/deleting files from one directory to another, in python ? Cheers
14
1804
by: cnb | last post by:
Are dictionaries the same as hashtables?
4
5959
by: Gilles Ganault | last post by:
Hello I fill two dictionaries with the same number of keys, and then need to compare the value for each key, eg. #Pour chaque APE, comparaison societe.ape.nombre et verif.ape.nombre import apsw #============ dic1={}
0
7993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
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
8401
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...
1
8054
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
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5867
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
3900
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...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.