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.iteritems():
# 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 5 2063
On May 7, 4:08 pm, brad <byte8b...@gmail.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.iteritems():
# 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.items():
if k not in second_dict or k in second_dict and k[v] !=
second_dict[k]:
let you know?
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
brad <by*******@gmail.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.iteritems():
# 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.iteritems():
for key in keys:
missing_keys.discard(key)
if first_dict.get(key, val) != val:
print "wrong value for", key, 'in', keys
if missing_keys:
print 'some keys are missing:', ',',join(remaining)
--
Arnaud
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.iteritems():
* *# 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.iteritems()) == \
sorted(((k[1], v) for k, v in second.iteritems()))
Google for DSU (Decorate-Sort-Undecorate)
HTH,
--
Miki <mi*********@gmail.com> http://pythonwise.blogspot.com
"Miki" <mi*********@gmail.comwrote in message
news:ad**********************************@b9g2000p rh.googlegroups.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.iteritems():
# 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.iteritems():
try:
if first_dict[key(1)] == value:
return True
else:
return False:
except KeyError:
return False
tjr This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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?
|
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...
|
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 = {}
|
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...
|
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...
|
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...
|
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...
|
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
|
by: cnb |
last post by:
Are dictionaries the same as hashtables?
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |