Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Simple exercise Date: Fri, 11 Mar 2016 10:38:01 +1100 Lines: 20 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de oUCl26iTtL+KSt2U2u3hgg2t2pqFOKQEC596fCLLAHQg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'cc:addr :python-list': 0.09; '0))': 0.09; 'meaningful': 0.09; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'gregory': 0.16; 'received:209.85.213.176': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'names.': 0.22; 'am,': 0.23; "haven't": 0.24; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'anywhere': 0.30; 'code': 0.30; 'instead,': 0.33; 'subject:Simple': 0.33; 'case,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'replaced': 0.35; 'something': 0.35; "isn't": 0.35; 'should': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'doing': 0.38; 'received:209': 0.38; 'mar': 0.65; 'chrisa': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=l7rQcQ/y6+bu4i5nMs0GVy7khXIEl28hw8Q1/xaqgZs=; b=F/Tnpkhd+0Lk1kLOK1WXyGBN9lFoYn/jNTFvFIGbhKKwWH7dAnPDABwESqNRkwIPVw iGr0A9IL3IUHfaEfI9mdTYTrjfVm9OE2b/FsOyI9yWhlRPwC91iFyaw2DV4C03ry2smh WhjnvvWFDcmVa0LNR2JDeXKYAGJi0dL+reNr/QkjS5oQhvcaerE8uMsrEtAaqafukn0j IElQhkhWIl6yFdqwJXhFKqE3Q4rjafqi090jTQQ0MSM+ogRMHcTfIBJz34eSpKYFepxz CKO9dZynW5xn7Kjl7m66uQlilzWV8kLkFvuNaxzOx+DNsBs7sdpZcfbBTZN4SKrNJft6 9qew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=l7rQcQ/y6+bu4i5nMs0GVy7khXIEl28hw8Q1/xaqgZs=; b=Re+icQD5yk3dBYz3Tya5U+jdz0YBckpUBhJ3IAVrRXOpMEOmFecfPqoolfGU7ft9v6 V+rX66MgC8i60Mx6VEWNd2WDUlO3XSr5eaarPiIrO3hbz4A11HwvpOt9ZaLXZGoULfpn wqQSf2K3jSJdOxR8OBsvi1M1Ifmw4MpEhVCN7jTR4kACr5UHXydYlC4UV4Y9sWFiWww6 kNdvpuJ/VrMAiR2GqEstqS1gEfESYixQYfZb+Lib4FqamRtD4qtNLFEhXuWC8n6ktoOz fX4zcSk4RZNI11B8P8GbPT8Gp9W8yFERSFkHyplOYgp73Hoasba8iIaXoa+7p0oLuAgm wpng== X-Gm-Message-State: AD7BkJJO/v9+SEvdn6lRD7x4fJxFQF9wP5M9CgwR2vPpeU3iw5/E3mcKPo9N2InmbBhxsaw9NNiRiEqQ2Ge9pA== X-Received: by 10.50.137.35 with SMTP id qf3mr1083303igb.92.1457653081471; Thu, 10 Mar 2016 15:38:01 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104561 On Fri, Mar 11, 2016 at 10:24 AM, Gregory Ewing wrote: > Instead, pull them out and give them meaningful names. > Doing so with the above code gives: > > name = m.group(1) > value = m.group(2) > if name not in od.keys(): > od[name] = int(value) > else: > od[name] += int(od.get(name, 0)) > > Now it's a lot eaier to see that you haven't used the > value anywhere in the second case, which should alert > you that something isn't right. Although in this case, the code is majorly redundant - and could be replaced entirely with a defaultdict(int). ChrisA