RE: Dict Copy & Compare



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@xxxxxxxxxx
[mailto:python-list-bounces+robert.rawlins=thinkbluemedia.co.uk@xxxxxxxxxx]
On Behalf Of Tim Golden
Sent: 30 April 2007 11:27
Cc: python-list@xxxxxxxxxx
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

.



Relevant Pages

  • Re: Help With PyParsing of output from win32pdhutil.ShowAllProcesses()
    ... None to a string). ... Still have an urge to parse with pyparsing? ... pyparsing Dict class. ... keys from the parsed data, so it looks like you are already thinking ...
    (comp.lang.python)
  • Re: Dictionary .keys() and .values() should return a set [with Python 3000 in mind]
    ... keyset of a dictionary be represented as a set instead of a list? ... is as much a product of historical progression as anything: lists and dicts ... so the collection of keys had to be returned as a list. ... of the elements in the dict.keys() list had no bearing on the dict, ...
    (comp.lang.python)
  • Re: Floats as keys in dict
    ... store the nodes I have in play as keys in a dict. ... the dict keys are then floats and I have to round the values ... give the same hash for two numbers that are "close enough" (e.g., ...
    (comp.lang.python)
  • Re: Newbie help with array handling
    ... The problem is that Python doesn't have a built-in sorted dictionary ... then you can just use a list of lists ... If you need the dict characteristics a lot, then you may use a dict ... (but keys must be hashable objects): ...
    (comp.lang.python)
  • Re: Sets in Python
    ... allow mutable items as keys is a bit arbitrary. ... The user doesn't need to know the mechanism, but the dict does. ... implemented as hash tables. ... Allow the hash of mutable objects to change, ...
    (comp.lang.python)