Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '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; 'python': 0.11; 'def': 0.12; "wouldn't": 0.14; "'rb')": 0.16; '__future__': 0.16; 'files:': 0.16; 'numpy': 0.16; 'subject:arithmetic': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; '2.x': 0.24; "shouldn't": 0.24; 'skip:{ 20': 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'this:': 0.26; 'subject:/': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'easier': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'usually': 0.31; 'division': 0.31; 'equality': 0.31; 'names.': 0.31; 'piece': 0.31; 'another': 0.32; 'open': 0.33; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'skip:s 30': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'shorter': 0.36; 'doing': 0.36; 'should': 0.36; 'list': 0.37; 'received:209': 0.37; 'skip:o 20': 0.38; 'feed': 0.38; 'needed': 0.38; 'little': 0.38; 'short': 0.38; 'most': 0.60; 'skip:o 30': 0.61; 'course': 0.61; "you're": 0.61; 'such': 0.63; 'skip:n 10': 0.64; '1st': 0.74; 'clearer': 0.84; 'improved.': 0.84; 'oscar': 0.84; 'carlos': 0.91; 'mean.': 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=//jWzlqNQlz9qH1iGGN+jckYaAMLOokmfIuAWN9XG5s=; b=dxsYOphHVu/IkOr0vAr78QsIi9Uq8+gF9ad52uqDRMFsbYxHIEm60a4hxegVFWqjsY 5wFrCzRClXsAcTNWRoI4cjOBno7D5d/rGtH76rUs/AA3qVbRUJLODyJ8KXf7fd/VDI6f PPcpl7OobGLxoFcLOPotMbH9oFIpZcE0TudbdAwslr9gXXQK4vj8P2u7EHoi+1inRzoi r7qf+wRjdv3LMxZx8OxuEAEO2Vzgt+LXDEtLounXP+vbMo592bDVnIkXmKfGqSvte+uI gbAtGO2K89bdS/bXjXXUcwytCePuC2x1wEE33u6BUoSVF5eW6999TMpiqXomLTGv2q3o 1bRA== X-Received: by 10.52.249.41 with SMTP id yr9mr4163326vdc.17.1369305458783; Thu, 23 May 2013 03:37:38 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Thu, 23 May 2013 11:37:18 +0100 Subject: Re: file I/O and arithmetic calculation To: Carlos Nepomuceno Content-Type: text/plain; charset=ISO-8859-1 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369305461 news.xs4all.nl 15995 [2001:888:2000:d::a6]:56755 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45795 On 23 May 2013 04:15, Carlos Nepomuceno wrote: > The last line of my noob piece can be improved. So this is it: Most of it can be improved. > 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(['File "{}" has 1st row average = {:.2f}'.format(n,a1r[i]) for i,n in enumerate(filenames) if s1c[i]==50]) You're writing repeated list comprehensions that feed into one another like this: list2 = [func1(x) for x in list1] list3 = [func2(y) for y in list2] list4 = [func3(y) for y in list2] In this case it is usually better to write a single loop for x in list1: y = func1(x) v = func2(y) w = func3(y) With that your code becomes: filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] for filename in filenames: contents = [[int(z) for z in y.split(',')] for y in open(filename).read().split()] s1c = sum([r[0] for r in contents]) a1r = sum(f[0])/float(len(contents[0])) if s1c == 50: print('File "{}" has 1st row average = {:.2f}'.format(filename,a1r)) However you shouldn't really be doing open(x).read().split() part. You should use the with statement to open the files: with open(filename, 'rb') as inputfile: contents = [map(int, line.split()) for line in inputfile] Of course if you don't have so many list comprehensions in your code then your lines will be shorter and you won't feel so much pressure to use such short variable names. It's also better to define a mean function as it makes it clearer to read: # Needed by the mean() function in Python 2.x from __future__ import division def mean(numbers): return sum(numbers) / len(numbers) filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] for filename in filenames: with open(filename, 'rb') as inputfile: matrix = [map(int, line.split()) for line in inputfile] column1 = [row[0] for row in matrix] row1 = matrix[0] if mean(column1) == 50: print('File "{}" has 1st row average = {:.2f}'.format(filename, mean(row1))) It's all a little easier if you use numpy: import numpy as np filenames = ['1.txt', '2.txt', '3.txt', '4.txt', '5.txt'] for filename in filenames: matrix = np.loadtxt(filename, dtype=int) column1 = matrix[:, 0] row1 = matrix[0, :] if sum(column1) == 50 * len(column1): print('File "{}" has 1st row average = {:.2f}'.format(filename, np.mean(row1))) Then again in practise I wouldn't be testing for equality of the mean. Oscar