Path: csiph.com!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.python Subject: Re: Simple exercise Date: Thu, 10 Mar 2016 11:30:13 +0100 Organization: PointedEars Software (PES) Lines: 30 Message-ID: <15045007.7fdgliee2q@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1457605813 19604 eJwFwYEBADAEA7CbmKLn2PD/CUtwXPyFOdyw2OSsaWEHINVQzoy5vSzV6siWleQ7c120Ph+hEU8= (10 Mar 2016 10:30:13 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Thu, 10 Mar 2016 10:30:13 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNzAkBACEIBMBKoLKLcfjsH+FuAoxtKIoHhmPPnqOucfPW+JtSNmSvmKInJyO85MBFxVa2iibCRMmO9n/5AFL8FTE= Cancel-Lock: sha1:W5ZbKYBdMmbc1K4YMATI2wWJiMo= X-NNTP-Posting-Host: eJwFwQEBwDAIAzBLZ9CCna2AfwlP4DQqg2BgsbTGak+cl7I7U3PSHLJxqWMehx+DXZeq+AEsUhGR Xref: csiph.com comp.lang.python:104498 Rodrick Brown wrote: > […] > if m: > if m.group(1) not in od.keys(): > od[m.group(1)] = int(m.group(2)) > else: > od[m.group(1)] += int(od.get(m.group(1),0)) > […] This program logic appears to be wrong as you are not adding the value that you just read to the dictionary entry for the key that you just read but the value that you had in the dictionary for that key before. Perhaps you were looking for this (I also optimized a bit): key = m.group(1) value = int(m.group(1)) if key not in od: od[key] = value else: od[key] += value But there is probably an even more pythonic way to do this. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.