Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: file I/O and arithmetic calculation

37 views
Skip to first unread message
Message has been deleted

Mark Lawrence

unread,
May 22, 2013, 12:23:43 PM5/22/13
to pytho...@python.org
On 22/05/2013 17:13, Keira Wilson wrote:
> Dear all,
>
> I would appreciate if someone could write a simple python code for the
> purpose below:
>
> I have five text files each of 10 columns by 10 rows as follows:
>
>
> |file_one= 'C:/test/1.txt'
> file_two= 'C:/test/2.txt'
> . . .
> file_five= 'C:/test/5.txt'|
>
> I want to calculate the mean of first row (10 elements) for each file (5
> files), if mean of first column (10 elements) of each file (5 files) is 50.
>
> Thank you in advance.
>
> Keira
>

Sorry but we don't do homework.

--
If you're using GoogleCrap� please read this
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

Carlos Nepomuceno

unread,
May 22, 2013, 5:05:15 PM5/22/13
to pytho...@python.org
Funny! I made a lot of assumptions regarding your requirements specification. Let me know if it isn't what you need:

### 1strow_average.py ###
#Assuming you have CSV (comma separated values) files such as:
#1.txt = '0,1,2,3,4,5,6,7,8,9\n' \
#        '10,11,12,13,14,15,16,17,18,19\n' \
#        '20,21,22,23,24,25,26,27,28,29\n' ...
#
# Usage: contents[file][row][column]
# contents[0]       : file '1.txt'
# contents[1][2]    : 3rd row of file '2.txt'
# contents[3][4][5] : 6th column of 5th row of file '4.txt'
# len(contents)     : quantity of files
# len(contents[4])  : quantity of lines in file '5.txt'
# len(contents[4][0]: quantity of values in the 1st line of file '5.txt'

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])


________________________________
> From: wilk...@gmail.com
> Date: Thu, 23 May 2013 01:13:19 +0900
> Subject: file I/O and arithmetic calculation
> To: pytho...@python.org

>
>
> Dear all,
>
> I would appreciate if someone could write a simple python code for the
> purpose below:
>
> I have five text files each of 10 columns by 10 rows as follows:
>
>
>
> file_one = 'C:/test/1.txt'

> file_two = 'C:/test/2.txt'
> . . .
> file_five = 'C:/test/5.txt'

>
> I want to calculate the mean of first row (10 elements) for each file
> (5 files), if mean of first column (10 elements) of each file (5
> files) is 50.
>
> Thank you in advance.
>
> Keira
>
>

> -- http://mail.python.org/mailman/listinfo/python-list

Oscar Benjamin

unread,
May 22, 2013, 7:43:08 PM5/22/13
to Carlos Nepomuceno, pytho...@python.org
On 22 May 2013 22:05, Carlos Nepomuceno <carlosne...@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

Carlos Nepomuceno

unread,
May 22, 2013, 7:49:31 PM5/22/13
to pytho...@python.org
----------------------------------------
> From: oscar.j....@gmail.com
[...]

>
> 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

Indeed, but for that you can use Pascal.

List comprehensions it's what Python does best!

The code is pretty obvious to me, I mean there's no obfuscation at all.

Carlos Nepomuceno

unread,
May 22, 2013, 7:57:21 PM5/22/13
to pytho...@python.org
># contents[3][4][5] : 6th column of 5th row of file '4.txt'

BTW, it should read

# contents[3][4][5] : 6th value of 5th row of file '4.txt'

Denis McMahon

unread,
May 22, 2013, 7:54:12 PM5/22/13
to
On Thu, 23 May 2013 01:13:19 +0900, Keira Wilson wrote:

> I would appreciate if someone could write a simple python code for the
> purpose below:

Didn't have your data, so couldn't verify it completely, but try this:

import re
def v(s):
l=len(s)
t=0.
for i in range(l):
t=t+(abs(ord(s[i]))*1.)
return t/(l*1.)
for n in range(5):
m="c:/test/"+str(n+1)+".txt"
f=open(m,"r")
d=[]
t=0.
for l in range(10):
d=d+[re.findall(r"[0-9.eE+-]+",f.readline())]
t=t+v(d[l][0])
f.close()
c=t/10.
if c==50.:
t=0.
for u in range(10):
t=t+v(d[0][u])
r=t/10.
print "%s C1: %f R1: %f"%(m,c,r)

--
Denis McMahon, denismf...@gmail.com

Carlos Nepomuceno

unread,
May 22, 2013, 8:02:09 PM5/22/13
to pytho...@python.org
----------------------------------------
> From: denismf...@gmail.com
[...]
> --
> http://mail.python.org/mailman/listinfo/python-list

Can you send it again without tabs?

Oscar Benjamin

unread,
May 22, 2013, 8:34:37 PM5/22/13
to Carlos Nepomuceno, pytho...@python.org
On 23 May 2013 00:49, Carlos Nepomuceno <carlosne...@outlook.com> wrote:
>
> The code is pretty obvious to me, I mean there's no obfuscation at all.

I honestly can't tell if you're joking.

Carlos Nepomuceno

unread,
May 22, 2013, 8:37:52 PM5/22/13
to pytho...@python.org
----------------------------------------
> From: oscar.j....@gmail.com
> Date: Thu, 23 May 2013 01:34:37 +0100
> Subject: Re: file I/O and arithmetic calculation
> To: carlosne...@outlook.com
> CC: pytho...@python.org

>
> On 23 May 2013 00:49, Carlos Nepomuceno <carlosne...@outlook.com> wrote:
>>
>> The code is pretty obvious to me, I mean there's no obfuscation at all.
>
> I honestly can't tell if you're joking.

I'm not! lol

Carlos Nepomuceno

unread,
May 22, 2013, 11:15:58 PM5/22/13
to pytho...@python.org
The last line of my noob piece can be improved. So this is it:

### 1strow_average.py ###
#Assuming you have CSV (comma separated values) files such as:
#1.txt = '0,1,2,3,4,5,6,7,8,9\n' \
#        '10,11,12,13,14,15,16,17,18,19\n' \
#        '20,21,22,23,24,25,26,27,28,29\n' ...
#
# Usage: contents[file][row][column]
# contents[0]       : file '1.txt'
# contents[1][2]    : 3rd row of file '2.txt'

# contents[3][4][5] : value on the 6th column of 5th row of file '4.txt'


# len(contents)     : quantity of files
# len(contents[4])  : quantity of lines in file '5.txt'
# len(contents[4][0]: quantity of values in the 1st line of file '5.txt'

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])

Oscar Benjamin

unread,
May 23, 2013, 6:37:18 AM5/23/13
to Carlos Nepomuceno, pytho...@python.org
On 23 May 2013 04:15, Carlos Nepomuceno <carlosne...@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

Chris Angelico

unread,
May 23, 2013, 7:35:50 AM5/23/13
to pytho...@python.org
On Thu, May 23, 2013 at 10:37 AM, Carlos Nepomuceno
<carlosne...@outlook.com> wrote:
> ----------------------------------------
>> From: oscar.j....@gmail.com
>> Date: Thu, 23 May 2013 01:34:37 +0100
>> Subject: Re: file I/O and arithmetic calculation
>> To: carlosne...@outlook.com
>> CC: pytho...@python.org
>>
>> On 23 May 2013 00:49, Carlos Nepomuceno <carlosne...@outlook.com> wrote:
>>>
>>> The code is pretty obvious to me, I mean there's no obfuscation at all.
>>
>> I honestly can't tell if you're joking.
>
> I'm not! lol

Reminds me of an episode of BBT where Sheldon's trying to figure out sarcasm.

Leonard: <obviously sarcastic comment>
Sheldon: "Was that sarcasm?"
Leonard: "Noooooo..."
Sheldon: "Was THAT sarcasm?"
Leonard: -- no answer needed.

Normally don't do this much list comprehension in an example. It may
be fine for the code, but it doesn't help explain stuff. :)

ChrisA
Message has been deleted

Denis McMahon

unread,
May 23, 2013, 4:12:43 PM5/23/13
to
On Thu, 23 May 2013 07:17:58 -0700, Keira Wilson wrote:

> Dear all who involved with responding to my question - Thank you so much
> for your nice code which really helped me.

Hold on a sec? Someone posted code that gave the correct answer to a
homework question?

--
Denis McMahon, denismf...@gmail.com

Carlos Nepomuceno

unread,
May 23, 2013, 6:22:32 PM5/23/13
to pytho...@python.org
----------------------------------------
> From: denismf...@gmail.com
[...]

>> Dear all who involved with responding to my question - Thank you so much
>> for your nice code which really helped me.
>
> Hold on a sec? Someone posted code that gave the correct answer to a
> homework question?
>
> --
> Denis McMahon, denismf...@gmail.com

Not sure what she've asked, but mine certainly do what I think she've asked.

Message has been deleted
0 new messages