Re: updating dictionaries from/to dictionaries

2008-08-14 Thread Brandon
(1) iterating over foo: for key in foo: foo[key] += bar.get(key, 0) (2) iterating over bar: for key in bar: foo[key] += bar[key] I (again) challenge you to say *why* you feel that the iterating over bar solution will not work. Well if you're going to be clever enough to iterate

RE: updating dictionaries from/to dictionaries

2008-08-14 Thread Edwin . Madari
. thanks Edwin -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Brandon Sent: Thursday, August 14, 2008 8:35 PM To: python-list@python.org Subject: Re: updating dictionaries from/to dictionaries (1) iterating over foo: for key in foo: foo[key] += bar.get

Re: updating dictionaries from/to dictionaries

2008-08-14 Thread John Machin
On Aug 15, 11:23 am, [EMAIL PROTECTED] wrote: if the values for any of the keys are None, both do not work as can be seen below!!. If any of the values are None, nothing can work. The OP justifiably (given his problem domain (bigram frequencies)) expects that all values will be integers. If

RE: updating dictionaries from/to dictionaries

2008-08-14 Thread Edwin . Madari
Sent: Thursday, August 14, 2008 9:24 PM To: 'Brandon'; python-list@python.org Subject: RE: updating dictionaries from/to dictionaries if the values for any of the keys are None, both do not work as can be seen below!!. since both loops are iterating over keys(), 'get' method which would return

Re: updating dictionaries from/to dictionaries

2008-08-14 Thread John Machin
On Aug 15, 11:45 am, [EMAIL PROTECTED] wrote: by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after copy.deepcopy stmt in this example.. bar['xtra'] = 0 and re-run My first posting in this

Re: updating dictionaries from/to dictionaries

2008-08-12 Thread John Machin
On Aug 12, 12:26 pm, Brandon [EMAIL PROTECTED] wrote: You are very correct about the Laplace adjustment. However, a more precise statement of my overall problem would involve training and testing which utilizes bigram probabilities derived in part from the Laplace adjustment; as I

Re: updating dictionaries from/to dictionaries

2008-08-12 Thread Brandon
On Aug 12, 7:26 am, John Machin [EMAIL PROTECTED] wrote: On Aug 12, 12:26 pm, Brandon [EMAIL PROTECTED] wrote: You are very correct about the Laplace adjustment. However, a more precise statement of my overall problem would involve training and testing which utilizes bigram

Re: updating dictionaries from/to dictionaries

2008-08-12 Thread John Machin
On Aug 13, 5:33 am, Brandon [EMAIL PROTECTED] wrote: On Aug 12, 7:26 am, John Machin [EMAIL PROTECTED] wrote: On Aug 12, 12:26 pm, Brandon [EMAIL PROTECTED] wrote: You are very correct about the Laplace adjustment. However, a more precise statement of my overall problem would involve

updating dictionaries from/to dictionaries

2008-08-11 Thread Brandon
Hi all, I am not altogether experienced in Python, but I haven't been able to find a good example of the syntax that I'm looking for in any tutorial that I've seen. Hope somebody can point me in the right direction. This should be pretty simple: I have two dictionaries, foo and bar. I am

Re: updating dictionaries from/to dictionaries

2008-08-11 Thread Calvin Spealman
for k in foo: foo[k] += bar.get(k, 0) On Mon, Aug 11, 2008 at 3:27 AM, Brandon [EMAIL PROTECTED] wrote: Hi all, I am not altogether experienced in Python, but I haven't been able to find a good example of the syntax that I'm looking for in any tutorial that I've seen. Hope somebody can

Re: updating dictionaries from/to dictionaries

2008-08-11 Thread John Machin
On Aug 11, 6:24 pm, Calvin Spealman [EMAIL PROTECTED] wrote: for k in foo:   foo[k] += bar.get(k, 0) An alternative: for k in bar: foo[k] += bar[k] The OP asserts that foo keys are a superset of bar keys. If that assertion is not true (i.e. there are keys in bar that are not in foo, your

Re: updating dictionaries from/to dictionaries

2008-08-11 Thread Steven D'Aprano
On Mon, 11 Aug 2008 00:27:46 -0700, Brandon wrote: This should be pretty simple: I have two dictionaries, foo and bar. I am certain that all keys in bar belong to foo as well, but I also know that not all keys in foo exist in bar. All the keys in both foo and bar are tuples (in the bigram

Re: updating dictionaries from/to dictionaries

2008-08-11 Thread Brandon
Harder to say what you want to do than to just do it. The truly terrible thing is when you know that's the case even as you're saying it. Thanks for the help, all! -- http://mail.python.org/mailman/listinfo/python-list

Re: updating dictionaries from/to dictionaries

2008-08-11 Thread John Machin
On Aug 12, 2:52 am, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: On Mon, 11 Aug 2008 00:27:46 -0700, Brandon wrote: This should be pretty simple: I have two dictionaries, foo and bar. I am certain that all keys in bar belong to foo as well, but I also know that not all keys

Re: updating dictionaries from/to dictionaries

2008-08-11 Thread Brandon
I wasn't sure about the update method either, since AFAICT (not far) the values would in fact update, not append as I needed them to. But the iteritems and get combo definitely worked for me. Thank you for the suggested link. I'm familiar with that page, but my skill level isn't so far along

Re: updating dictionaries from/to dictionaries

2008-08-11 Thread John Machin
On Aug 12, 9:14 am, Brandon [EMAIL PROTECTED] wrote: I wasn't sure about the update method either, since AFAICT (not far) the values would in fact update, not append as I needed them to. append? Don't you mean add??? But the iteritems and get combo definitely worked for me. Under some

Re: updating dictionaries from/to dictionaries

2008-08-11 Thread Brandon
John: append? Don't you mean add??? Yes, that is what I meant, my apologies. What you need to do is practice translating from your requirements into Python, and it's not all that hard: run a loop through foo - for key in foo: match any of its keys that also exist in bar - if key in bar: