file I/O and arithmetic calculation

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu May 23 06:37:18 EDT 2013


On 23 May 2013 04:15, Carlos Nepomuceno <carlosnepomuceno at outlook.com> 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



More information about the Python-list mailing list