Re: [Tutor] adding dictionary values

2009-03-21 Thread عماد نوفل
2009/3/20 Kent Johnson > 2009/3/20 Emad Nawfal (عماد نوفل) : > > > if I want to do this with more than two dictionaries, the obvious > solution > > for me is to use something like the reduce functions with a list of > > dictionary names like: > > dictList = [dict1, dict2, dict3] > > newDict = red

Re: [Tutor] adding dictionary values

2009-03-20 Thread Kent Johnson
2009/3/20 Emad Nawfal (عماد نوفل) : > if I want to do this with more than two dictionaries, the obvious solution > for me is to use something like the reduce functions with a list of > dictionary names like: > dictList = [dict1, dict2, dict3] > newDict = reduce(addDicts, dictList) > > Is this a sa

Re: [Tutor] adding dictionary values

2009-03-20 Thread عماد نوفل
On Fri, Mar 20, 2009 at 12:03 PM, Richard Lovely wrote: > 2009/3/20 Chris Fuller : > > You should iterate over the keys of the dictionary: > > for k in a.keys(): > > because if you iterate over the full dictionary, your items are the > values, > > not the keys. Otherwise your code looks correct,

Re: [Tutor] adding dictionary values

2009-03-20 Thread Richard Lovely
2009/3/20 Chris Fuller : > You should iterate over the keys of the dictionary: > for k in a.keys(): > because if you iterate over the full dictionary, your items are the values, > not the keys.  Otherwise your code looks correct, and I don't think its > terribly bad form.  You could do something in

Re: [Tutor] adding dictionary values

2009-03-20 Thread Kent Johnson
2009/3/20 greg whittier : > This looks like it will work, but you can accomplish this more compactly by > just looping over the items in both dictionaries and making use of the > default argument of the dictionaries get method. > > newDict = {} > for k, v in dict1.items() + dict2.items(): >     ne

Re: [Tutor] adding dictionary values

2009-03-20 Thread Chris Fuller
Oops! The dictionary iterates over keys, not values as I stated (and demonstrated by your working code). Consequently, the example I gave could be more succinctly expressed by: sa = set(a) sb = set(b) Sorry for the error. Cheers ___ Tutor maillist -

Re: [Tutor] adding dictionary values

2009-03-20 Thread Chris Fuller
You should iterate over the keys of the dictionary: for k in a.keys(): because if you iterate over the full dictionary, your items are the values, not the keys. Otherwise your code looks correct, and I don't think its terribly bad form. You could do something interesting with sets: sa = set(a.k

Re: [Tutor] adding dictionary values

2009-03-20 Thread greg whittier
2009/3/20 Emad Nawfal (عماد نوفل) > Hi Tutors, > I have two pickled dictionaries containing word counts from two different > corpora. I need to add the values, so that a word count is the sum of both. > If the word "man" has a count of 2 in corpus A and a count of 3 in corpus B, > then I need a n