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 12:50:46 +1000 Lines: 142 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 2Hsnsnt608ggv1nNlpZN5AKoMlXbOIvjc+xE48HGNyYQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'yet.': 0.03; '[],': 0.07; 'counting': 0.07; 'inserts': 0.07; 'method,': 0.07; 'cc:addr:python-list': 0.09; 'addr': 0.09; 'collections': 0.09; 'iterate': 0.09; 'length.': 0.09; 'output,': 0.09; 'unpacking': 0.09; 'times,': 0.13; 'things.': 0.15; '2016': 0.16; 'bit.': 0.16; 'cares': 0.16; 'dfs': 0.16; 'fold': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'iteration,': 0.16; 'iteration.': 0.16; 'list1,': 0.16; 'mixture': 0.16; 'numbered': 0.16; 'plural,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'recipe': 0.16; 'sequence.': 0.16; 'sift': 0.16; 'singular': 0.16; "someone's": 0.16; 'stored.': 0.16; 'wrote:': 0.16; 'flexibility': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'sorry,': 0.22; 'pass': 0.22; 'trying': 0.22; 'am,': 0.23; 'code.': 0.23; 'represents': 0.23; 'split': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; "doesn't": 0.26; 'example': 0.26; 'chris': 0.26; 'not.': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'cool': 0.27; 'idea': 0.28; '"do': 0.29; 'sure,': 0.29; 'that.': 0.30; 'work.': 0.30; 'skip:[ 10': 0.31; 'operations': 0.31; 'regardless': 0.31; 'probably': 0.31; 'skip:s 30': 0.31; "can't": 0.32; 'addresses': 0.32; 'statement': 0.32; 'maybe': 0.33; 'options': 0.33; 'class': 0.33; 'is?': 0.33; 'items.': 0.33; 'lets': 0.33; 'ordered': 0.33; 'picking': 0.33; 'rule': 0.33; 'case,': 0.34; 'lists': 0.34; 'that,': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'skip:c 30': 0.35; 'could': 0.35; 'lists.': 0.35; 'something': 0.35; 'item': 0.35; 'but': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'doing': 0.38; 'skip:z 10': 0.38; "won't": 0.38; 'received:209': 0.38; 'stuff': 0.38; 'building': 0.38; 'someone': 0.38; 'mean': 0.38; 'represent': 0.38; 'why': 0.39; 'data': 0.39; 'build': 0.40; 'where': 0.40; 'still': 0.40; 'easy': 0.60; 'street,': 0.60; 'address': 0.61; 'side': 0.62; 'back': 0.62; 'different': 0.63; 'you.': 0.64; 'city': 0.65; 'remember,': 0.66; 'state,': 0.66; "they're": 0.66; 'bulk': 0.76; 'saw': 0.77; '9:36': 0.84; 'chrisa': 0.84; 'cream': 0.84; 'discrete': 0.84; 'to:none': 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=xgRF5vDx9Md+kyWFW32zJZ2i3Ax0HTWmaOt4gGBTbVA=; b=gvytqU2TydTFuCh/IEt4KFNgLQfbUMgJKpeFw4xzDBnJI5TsGcqrvL6YIRNLbZe9i3 tEj0WTtKuklEeFcZ238hw3n9EtzjomMiENoNvbx488xm2rXOJL4C44dMFongHuJ7HBcB WP5n+Wb/HcaGeGuzfWHRVBztmw1YAy1YEulqQXT1Gagiyu6h+rw+1JTkToRN6trhY/qO SNHuf25GNl1XPQar7F5X1jfVxlByqC11X9//l8sTKWkdh/FG4qENSeDhWIve8Er2PwUB vaDINmtkOCVprZhpgbovuhqSZKKbb1URkV5q3Rd5zobkuSRRPM211bebXYrk2yvZeWRk +7XA== 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=xgRF5vDx9Md+kyWFW32zJZ2i3Ax0HTWmaOt4gGBTbVA=; b=JQt0rX66aAnEh2287mBiik0L9prrftmphEjytelApRpdpCGZkmbR8o5alNHEvzEo8L XfINd1F4auSkFfQ20p+yMHtw8PdjpB5oKnswSkyIFs3ugHgQ5KcN4wvGXvV2Sq6NtSM6 Mo0uCBUJhTCFoDxpctBoPf6Kv0bLQlsL+yUYhYFYBxuWR+XaidYIWdEXKLeF7n+aItn+ oPS96j6xPc/ozserc+Z8CYVYDwzuHg0Emar+JfmCKgpt7ercUCXaa4iZe0PWSrfjqRHe WmU8CVu/2nuWhXC+zVpYKQ5vFPP7K8J4CAY9NppXnLE8iGSNXTdfAB8JxgYoxMEXj3gb 66og== X-Gm-Message-State: AOPr4FU6RiHt8SFgxQF6sAdgIBPsJI0dnx/xaP0e0oCUjmvmpH0jadl/p6JeB9P+QqGgg9OpbAwV1Ga4y3lsaA== X-Received: by 10.112.205.69 with SMTP id le5mr12535719lbc.138.1462675846800; Sat, 07 May 2016 19:50:46 -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:108321 On Sun, May 8, 2016 at 12:15 PM, DFS wrote: > On 5/7/2016 9:36 PM, Chris Angelico wrote: >> >> On Sun, May 8, 2016 at 11:16 AM, DFS wrote: >>> >>> 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. > > I like the idea of one iteration, but how? (I'll be trying myself before I > check back in) > > Remember, it's required to split the data up, to give flexibility in > sorting, searching, output, etc. Start by unpacking the comprehensions into statement form. street = [] for s in addr: street.append(s.split(',')[0]) city = [] for c in addr: city.append(c.split(',')[1].strip()) state = [] for s in addr: state.append(s[-8:][:2]) zipcd = [] for z in addr: zipcd.append(z[-5:]) Now see how you're doing the same thing four times? Let's start by keeping it the way it is, but combine the loops. street, city, state, zipcd = [], [], [], [] for a in addr: street.append(a.split(',')[0]) city.append(a.split(',')[1].strip()) state.append(a[-8:][:2]) zipcd.append(a[-5:]) Side point: I prefer collections to be named in the plural, so these would be "streets", and "addrs". This lets you follow a very simple rule of iteration: "for item in collection" or "for singular in plural". In this case, "for address in addresses" is classic iteration. So, now that you have a single loop picking up the different pieces, it's easy to build up a simple object that represents an address. # Either this from collections import namedtuple Address = namedtuple("Address", ["street", "city", "state", "zipcd"]) # or this from types import SimpleNamespace class Address(SimpleNamespace): pass addresses = [] for a in addr: addresses.append(Address( street=a.split(',')[0], city=a.split(',')[1].strip(), state=a[-8:][:2], zipcd=a[-5:], ) Voila! One iteration, and a single object representing an address. > I saw a cool example where someone built a list and used it to do a bulk > INSERT. That probably won't work well here, because one of the options I > give the user is # of addresses to store. So I do invididual INSERTs using > the 'for j in range()' method, which makes it easier to track how many > addresses have been stored. You could slice it if you actually want that. >>> 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. > > Sorry, but I don't understand what you mean by insignificant. j keeps track > of the position in the list - regardless of the length of the list. Right, but *who cares* what the position is? All you want to do is the "do something" bit. Don't think in terms of concrete and discrete operations in a computer; think in the abstract (what are you trying to accomplish?), and then represent that in code. >> 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. > > Again, not following you. > > > The only reason > > for j in range(len(list1)): > do something with list1[j], list2[j], list3[j], etc. > > or > > for item1, item2, item3 in zip(list1, list2, list3): > do something with the items > > works is because each list has the same number of items. Sure, but who cares what each item's position is? All that matters is that they have corresponding positions, which is what zip() does. Imagine you don't even have the whole lists yet. Imagine someone's still writing stuff to them as you work. Maybe they're infinite in length. You can't iterate up to the length of list1, because it doesn't HAVE a length. But you can still zip it up with other parallel collections, and iterate over them all. ChrisA