RE: Dict Copy & Compare
- From: "Robert Rawlins - Think Blue" <robert.rawlins@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Apr 2007 12:09:18 +0100
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,dicts,
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
so when I say 'unique keys in dict 1' I basically mean all those keys thatfollowing
are in dict one but not in dict 2. So imagine my 2 dicts with the
keys.stuff
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
very well, being so new to dicts its easy to get confused with my terms.time
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
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
.
- Prev by Date: Re: I/O Operations .....
- Next by Date: Re: if __name__ == 'main': & passing an arg to a class object
- Previous by thread: Re: Dict Copy & Compare
- Next by thread: Re: Dict Copy & Compare
- Index(es):
Relevant Pages
|