Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13123
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python.list@tim.thechases.com> |
| 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; 'else:': 0.03; 'mess': 0.07; 'terry': 0.07; 'csv': 0.09; 'first:': 0.09; 'loop.': 0.09; '*before*': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'headers:': 0.16; 'lambda': 0.16; 'message-id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'reedy': 0.16; 'row': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'item.': 0.23; 'variable': 0.24; 'times,': 0.24; 'code': 0.25; 'posted': 0.26; 'skip:( 20': 0.29; 'cc:addr:python.org': 0.30; 'separately.': 0.30; 'list': 0.32; 'headers': 0.32; "isn't": 0.33; 'header:User-Agent:1': 0.34; 'flag': 0.34; 'subject:next': 0.34; 'with.': 0.37; 'several': 0.37; 'but': 0.37; 'subject:: ': 0.39; 'header': 0.39; "it's": 0.40; 'where': 0.40; 'processing': 0.40; 'love': 0.62; 'traditional': 0.64 |
| Date | Sun, 11 Sep 2011 07:41:58 -0500 |
| From | Tim Chase <python.list@tim.thechases.com> |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.20) Gecko/20110826 Icedove/3.1.12 |
| MIME-Version | 1.0 |
| To | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Idioms combining 'next(items)' and 'for item in items:' |
| References | <j4gea4$m3b$1@dough.gmane.org> |
| In-Reply-To | <j4gea4$m3b$1@dough.gmane.org> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-AntiAbuse | This header was added to track abuse, please include it with any abuse report |
| X-AntiAbuse | Primary Hostname - boston.accountservergroup.com |
| X-AntiAbuse | Original Domain - python.org |
| X-AntiAbuse | Originator/Caller UID/GID - [47 12] / [47 12] |
| X-AntiAbuse | Sender Address Domain - tim.thechases.com |
| X-Source | |
| X-Source-Args | |
| X-Source-Dir | |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.984.1315744926.27778.python-list@python.org> (permalink) |
| Lines | 46 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1315744926 news.xs4all.nl 2466 [2001:888:2000:d::a6]:35035 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:13123 |
Show key headers only | View raw
On 09/10/11 14:36, Terry Reedy wrote:
> 1. Process first item of an iterable separately.
>
> A traditional solution is a flag variable that is tested for each item.
>
> first = True
> <other setup>
> for item in iterable:
> if first:
> <process first>
> first = False
> else:
> <process non-first>
>
> (I have seen code like this posted on this list several times, including
> today.)
>
> Better, to me, is to remove the first item *before* the loop.
>
> items = iter(iterable)
> <set up with next(items)
> for item in items:
> <process non-first>
I like to use this one for processing CSV files where I need to
clean up the headers:
r = csv.reader(f)
headers = r.next()
header_map = dict(
(header.strip().upper(), i)
for i, header
in enumerate(headers)
)
for row in r:
item = lambda s: row[header_map[s]].strip()
thing = item("THING")
whatever = item("WHATEVER")
It's mostly like a DictReader, but it isn't as sensitive to the
spaces/capitalization that clients love to mess with.
-tkc
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Idioms combining 'next(items)' and 'for item in items:' Tim Chase <python.list@tim.thechases.com> - 2011-09-11 07:41 -0500
csiph-web