Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45757

Re: file I/O and arithmetic calculation

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.98; '*S*': 0.01; 'skip:[ 20': 0.04; 'subject:file': 0.07; 'filename': 0.09; 'filenames': 0.09; 'filenames:': 0.09; 'repeated': 0.09; 'cc:addr:python-list': 0.11; "wouldn't": 0.14; 'read?': 0.16; 'subject:arithmetic': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'print': 0.22; 'certainly': 0.24; 'skip:{ 20': 0.24; 'cc:2**0': 0.24; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'file': 0.32; 'something': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'rather': 0.38; 'easy': 0.60; '1st': 0.74; 'oscar': 0.84; 'carlos': 0.91; 'average': 0.93; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=fJGqpnHeuruIYG3RTp3Gin1igX9sZaR1lKr23wV8OMw=; b=fyYEbghwzClWJBmvi0EjLt8CImJfq138clcvoPQRsUjy0FS3L602Akp/GGApW9u1Uu 0t/S/hIClshMzzKwvNGl/xayDCf02kylZMBHLquXAmJDMUI4osSmdYYYqZ/xL1WxCWCs +E3lCJ8N/pnvr7OPr1rFZepCA9xtol+2DefLD5dRD0qSwWRHRl4Bi7ekAiwEffui/MRH X+YkI9+FjJTT6y/m0i7War3gTB2Dxo6N2+IDnis5TJkGWIIAKQbV2IeIKgykjrKyfraR ia+lajXAlXh+xFOdRiWCXT+S34zuEn7GfcSgwLZBDk+ZAejv5T2Tv+6MQH9n8V7Kw+B8 j3hw==
X-Received by 10.52.117.16 with SMTP id ka16mr426777vdb.43.1369266208265; Wed, 22 May 2013 16:43:28 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <BLU176-W17E5DA2B650B3CE80F79BFD7A90@phx.gbl>
References <CAK9meDsND9501=2ydmCAFi4s6+fw+rTrDGJA2qpEyXKoae6Axw@mail.gmail.com> <BLU176-W17E5DA2B650B3CE80F79BFD7A90@phx.gbl>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Thu, 23 May 2013 00:43:08 +0100
Subject Re: file I/O and arithmetic calculation
To Carlos Nepomuceno <carlosnepomuceno@outlook.com>
Content-Type text/plain; charset=ISO-8859-1
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.1979.1369266216.3114.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369266216 news.xs4all.nl 15960 [2001:888:2000:d::a6]:50151
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45757

Show key headers only | View raw


On 22 May 2013 22:05, Carlos Nepomuceno <carlosnepomuceno@outlook.com> wrote:
>
> filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt']
> contents  = [[[int(z) for z in y.split(',')] for y in open(x).read().split()] for x in filenames]
> s1c  = [sum([r[0] for r in f]) for f in contents]
> a1r  = [sum(f[0])/float(len(f[0])) for f in contents]
> print '\n'.join([x for x in ['File "{}" has 1st row average = {:.2f}'.format(n,a1r[i]) if s1c[i]==50 else '' for i,n in enumerate(filenames)] if x])

Do you find this code easy to read? I wouldn't write something like
this and I certainly wouldn't use it when explaining something to a
beginner.

Rather than repeated list comprehensions you should consider using a
single loop e.g.:

for filename in filenames:
    # process each file

This will make the code a lot simpler.


Oscar

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: file I/O and arithmetic calculation Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-05-23 00:43 +0100

csiph-web