Dict Copy & Compare

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Mon Apr 30 06:12:54 EDT 2007


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.

Thanks again for all your help Tim,

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 10:15
Cc: python-list at python.org
Subject: Re: Dict Copy & Compare

Robert Rawlins - Think Blue wrote:
> I have two dicts, one named 'this' and the other named 'that'.
> 
> I want to get all the unique keys from 'this' and log them into a file, I
> then want to take all the unique values from 'that' and log them into a
> separate file.

Couple of points which are confusing me:

1) Any dict can *only* have unique keys, ie you can't have
a key appearing more than once in a dictionary by
definition.

2) You speak of unique keys in "this" but unique values
in "that". Is that deliberate on your part? Might be, but
I'm not quite clear.

> I have functions set up for the logging, so I can call it like
> logThis(uniquekey) and logThat(uniquekey).

Here you refer to "uniquekey" in both cases, so maybe a
mistake above?

> So it's just a case of firstly returning a list of all keys that are in
> 'this' but NOT in 'that' and then visa versa, then loop over them
performing
> the function.

OK, well following by example earlier:

<code>
d1 = dict (a=1, b=2, c=3)
d2 = dict (b=4, c=5, d=6)

s1 = set (d1) # => set of 'a', 'b', 'c'
s2 = set (d2) # => set of 'b', 'c', 'd'

s1_not_in_s2 = s1 - s2 # => set of 'a'
s2_not_in_s1 = s2 - s1 # => set of 'd'

for key in s1_not_in_s2:
   print key, "=>", d1[key]

for key in s2_not_in_s1:
   print key, "=>", d2[key]

</code>

Obviously there are more concise ways of representing
this; I'm just spelling the whole thing out to make it
clearer (I hope). If this approach seems fruitful, have
a look at the set typeit's a recentish addition to
Python but very useful for this kind of thing:

   http://docs.python.org/lib/types-set.html

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




More information about the Python-list mailing list