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 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?
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*******@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
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
"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 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 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...
|
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 a string
bsddb requires that both the key,value are string.
shelve does support values being object but not the keys. Is there any
|
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...
| |
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:
|
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?
|
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 apsw
#============
dic1={}
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |