From: rurpy on 13 Mar 2010 11:13 On Mar 13, 8:28 am, Patrick Maupin wrote:> On Mar 13, 9:05 am, vsoler wrote: > > > Say that "m" is a tuple of 2-tuples > > > m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6)) > > > and I need to build a "d" dict where each key has an associated list > > whose first element is the count, and the second is the sum. If a 2- > > tuple contains a None value, it should be discarded. > > > The expected result is: > > d={'as':[2, 9], 'ab': [1,5]} > > > How should I proceed? So far I have been unsuccessful. I have tried > > with a "for" loop. > > Post your first try at a for loop, and people might be willing to > point out problems, but this is such a basic for loop that it is > unlikely that anybody is going to write your ENTIRE homework for you. This is probably what you (OP) were trying to come up with? [untested] d = {} for item in m: key = m[0]; value = m[1] if key is None or value is None: continue if key not in dict: d[key] = [value] else: d[key].append (value) You can replace the for item in m: key = m[0]; value = m[1] above with for key, value in m: which is a little nicer. However, as other responses point out, when you want to "accumulate" results in a dict, collections.defaultdict should pop into your mind first. From: rurpy on 13 Mar 2010 11:26 On Mar 13, 9:13 am, ru...(a)yahoo.com wrote:> On Mar 13, 8:28 am, Patrick Maupin wrote: > > > > > On Mar 13, 9:05 am, vsoler wrote: > > > > Say that "m" is a tuple of 2-tuples > > > > m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6)) > > > > and I need to build a "d" dict where each key has an associated list > > > whose first element is the count, and the second is the sum. If a 2- > > > tuple contains a None value, it should be discarded. > > > > The expected result is: > > > d={'as':[2, 9], 'ab': [1,5]} > > > > How should I proceed? So far I have been unsuccessful. I have tried > > > with a "for" loop. > > > Post your first try at a for loop, and people might be willing to > > point out problems, but this is such a basic for loop that it is > > unlikely that anybody is going to write your ENTIRE homework for you. > > This is probably what you (OP) were trying to come up with? > [untested] > > d = {} > for item in m: >     key = m[0];  value = m[1] >     if key is None or value is None: continue >     if key not in dict: >         d[key] = [value] >     else: >         d[key].append (value) > > You can replace the >   for item in m: >       key = m[0];  value = m[1] > above with >   for key, value in m: > which is a little nicer. > > However, as other responses point out, when you want > to "accumulate" results in a dict, collections.defaultdict > should pop into your mind first. Oops, didn't read very carefully, did I? That should be: d = {} for item in m: key = m[0]; value = m[1] if key is None or value is None: continue if key not in dict: d[key] = [1, value] else: d[key][0] += 1 d[key][1] += value From: rurpy on 13 Mar 2010 12:16 On Mar 13, 9:26 am, ru...(a)yahoo.com wrote:> That should be: > d = {} > for item in m:     key = item[0];  value = item[1]>     if key is None or value is None: continue >     if key not in dict: >         d[key] = [1, value] >     else: >         d[key][0] += 1 >         d[key][1] += value That's it. Any other mistakes, you find 'em. From: vsoler on 13 Mar 2010 16:42 On 13 mar, 18:16, ru...(a)yahoo.com wrote:> On Mar 13, 9:26 am, ru...(a)yahoo.com wrote:> That should be: > > d = {} > > for item in m: > >       key = item[0];  value = item[1] > > >     if key is None or value is None: continue > >     if key not in dict: > >         d[key] = [1, value] > >     else: > >         d[key][0] += 1 > >         d[key][1] += value > > That's it.  Any other mistakes, you find 'em. Thank you all. Your answers are more than valuable to me. I'll study them carefully, but no doubt, my post has been answered. By the way, I suppose I am the OP. Since I am not an native English speaking person, I do not know what it stands for. Perhaps you can tell me. From what I see from your posts, you would have preferred that I included in my original post my "for loop", so that the post is not so abstract. I have taken note and I'll make it better next time. Thank you for your help. Vicente Soler From: rurpy on 13 Mar 2010 17:04 On Mar 13, 2:42 pm, vsoler wrote: > By the way, I suppose I am the OP. Since I am not an native English > speaking person, I do not know what it stands for. Perhaps you can > tell me. OP means Original Poster (the person who started the discussion) or sometimes Original Post, depending on context.