Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: pylint woes Date: Sun, 8 May 2016 11:36:26 +1000 Lines: 53 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de TS0/wPidoF9M6+zwQ9QE7w04vL+6iWY34LXLrAbr4Utw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.03; 'counting': 0.07; 'cc:addr:python-list': 0.09; 'addr': 0.09; 'collections': 0.09; 'iterate': 0.09; 'times,': 0.13; 'things.': 0.15; '2016': 0.16; 'dfs': 0.16; 'fold': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'mixture': 0.16; 'numbered': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'recipe': 0.16; 'sequence.': 0.16; 'sift': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'parse': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'not.': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:[ 10': 0.31; 'possibly': 0.32; 'ordered': 0.33; 'lists': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'lists.': 0.35; 'something': 0.35; 'instead': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'suggestion': 0.37; 'list.': 0.37; 'doing': 0.38; 'received:209': 0.38; 'names': 0.38; 'building': 0.38; 'why': 0.39; 'data': 0.39; 'street,': 0.60; 'your': 0.60; 'address': 0.61; 'city': 0.65; 'website:': 0.65; 'state,': 0.66; "they're": 0.66; 'chrisa': 0.84; 'cream': 0.84; 'received:209.85.215.42': 0.84; 'to:none': 0.91; 'rd,': 0.91; 'imagine': 0.96 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=zxcr/0HQCZvMrh+3gEGP0zxdLW5RQnpAP4bULMrqqHs=; b=PwVKIfd3DAYvl3kaSKOgUwfDfM6ndIRf/Bl6+Znx7JNWDmRx2QyI6QvezdMWuKUxp6 XB/7P0eIYum3aJtbIiAMd3d1WJwRfDw/wYmFPWxsU2AeGH2V/HtRhcbch1lk+o1ScQQp WFD2Qq56vsPwyCMGOIlUk+O9qzsW5+KBX/177QiOARfpilN29u38ZaDMtJ6ArthRQMcz zGJGhiBDjhZOnhrXBLMlgVpgkeEmWZW5pskAmoRSVPTtlRLA2zyYyfTGLz3GltSN3hBr Wp0CC/kH0SG2oGDcen6Vb469BoiAs02FsY/aMovxyLczwOaB8c+Qv0ybxi4dPopQ/57A PIvg== 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=zxcr/0HQCZvMrh+3gEGP0zxdLW5RQnpAP4bULMrqqHs=; b=Hi1FEFSrwm7r3dLud3JNVGMjRWaSe8MmjN0GYQDm/9o0ay01bbKV+Fp23eY1J8C+8C BB2SneVoMl7nh9fPtDQpdQ529ORagNnT5Y/TEvtJHKac0Uq902RsEQIJ3Gh6uKByP/TQ osTPVFeq+G04oOAMIqdN3dsc9Hn/4nsvwdnBU1z/Fuhm9u+ovH1QucM4WDrlcyEJqrwM NtIv6D8ocGc+12hx+rNCMWHZS1oxc7CjE6Fn6unSOStooCImk+V+H60gxzl/UzuWg3n+ 2l58oAI0w3oBg9pkcmXwdOLp2WlMzkWSBJC002Ti662Nba3U1nlH4MhbDRz6DnT8VNJJ a69w== X-Gm-Message-State: AOPr4FVNwQjbZmUp5yjNKa8fPxV5uDBWa2XFanQTf/nG7T3ejGxCUzzkzc+IiMWLxD/+G/6VNwyRGEbHNSEYMg== X-Received: by 10.25.148.69 with SMTP id w66mr1908486lfd.28.1462671386514; Sat, 07 May 2016 18:36:26 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:108313 On Sun, May 8, 2016 at 11:16 AM, DFS wrote: > On 5/7/2016 1:01 PM, Chris Angelico wrote: >> The suggestion from a human would be to use zip(), or possibly to >> change your data structures. > > > Happens like this: > > address data is scraped from a website: > > names = tree.xpath() > addr = tree.xpath() > > I want to store the data atomically, so I parse street, city, state, and zip > into their own lists. > > "1250 Peachtree Rd, Atlanta, GA 30303 > > street = [s.split(',')[0] for s in addr] > city = [c.split(',')[1].strip() for c in addr] > state = [s[-8:][:2] for s in addr] > zipcd = [z[-5:] for z in addr] So you're iterating over addr lots of times, and building separate lists. As an alternative, you could iterate over it *once*, and have a single object representing an address. > Why is it better to zip() them up and use: > > for item1, item2, item3 in zip(list1, list2, list3): > do something with the items > > than > > > for j in range(len(list1)): > do something with list1[j], list2[j], list3[j], etc. Because 'j' is insignificant here, as is the length of the list. What you're doing is iterating over three parallel lists - not counting numbers. Imagine that, instead of lists, you just have *sequences* - ordered collections of things. You can follow a recipe without knowing the numbers of the individual lines; you just need to know the sequence. Here, iterate over this collection: * Collect ingredients. * Cream the butter and the sugar. * Sift the salt into the flour. * Fold the mixture into an origami crane. These instructions work whether they're numbered or not. ChrisA