Dict Copy & Compare

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Mon Apr 30 07:09:18 EDT 2007


Thanks for that Tim,

Don't feel guilty mate, I've learned a little something from you anyway,
whether its applied here or not.

On quick question, how can I order a dict by the 'values' (not keys) before
looping? Is that possible?

Thanks,

Rob

-----Original Message-----
From: python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org
[mailto:python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org]
On Behalf Of Tim Golden
Sent: 30 April 2007 11:27
Cc: python-list at python.org
Subject: Re: Dict Copy & Compare

Robert Rawlins - Think Blue wrote:
> Hello Tim,
> 
> Sorry, that 'value' was a slip up on my part, we're just dealing with keys
> here.
> 
> I get that a dict stores unique keys only but we're comparing the two
dicts,
> so when I say 'unique keys in dict 1' I basically mean all those keys that
> are in dict one but not in dict 2. So imagine my 2 dicts with the
following
> keys.
> 
> Dict 1			Dict 2
> ------			-------
> 00:00:00:00		00:00:00:00
> 11:11:11:11		11:11:11:11
> 22:22:22:22		33:33:33:33
> 44:44:44:44		44:44:44:44
> 55:55:55:55
> 
> Now, 22:22:22:22 and 55:55:55:55 is unique to dict one, and 33:33:33:33 is
> unique to dict 2, does that make sense? Sorry for not explaining this
stuff
> very well, being so new to dicts its easy to get confused with my terms.
> 
> I then want to pass those keys as a string value into my function as an
> argument, like.
> 
> thisFunction('22:22:22:22')
> thisFunction('55:55:55:55')
> 
> thatFunction('33:33:33:33')
> 
> I'm hoping that your method will work for me, I've just got to spend my
time
> understanding what each step of it does.

Well I feel a bit guilty now I look back at your
original post, because I've probably given
you a more complex solution than you really need. Your
initial approach is probably quite adequate. Python
dicts are highly tuned beasts so unless you're doing
something really big or bizarre, you can sensibly do:

<code>
d1 = {
   "00:00:00:00" : None,
   "11:11:11:11" : None,
   "22:22:22:22" : None,
   "44:44:44:44" : None,
   "55:55:55:55" : None
}

d2 = {
   "00:00:00:00" : None,
   "11:11:11:11" : None,
   "33:33:33:33" : None,
   "44:44:44:44" : None
}

for k in d1:
   if d1 not in d2:
     thisFunction (d1)

for k in d2
   if d2 not in d1:
    thatFunction (k)

</code>

But even if this is adequate for your purposes,
it's always good to be aware of what's in your
programming toolbox and there's always the danger
you'll end up implementing sets in dicts (which
is what everyone did before Python 2.3).

TJG
-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list