File tree 3 files changed +51
-0
lines changed
3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ # avg.py -- computes the column-wise average and standard deviation of a datafile
2
+ # with an arbitrary number of parallel columns of data
3
+ #
4
+ # an exercise in learning python
5
+ #
6
+ # cameron f abrams
7
+ # cfa22@drexel.edu
8
+ # (c) 2017
9
+ # drexel university
10
+ #
11
+ from math import sqrt # so we can use math functions without "math.*"
12
+ import fileinput # can only use fileinput functions with "fileinput.*"
13
+ #import sys
14
+ import string
15
+ import numpy as np
16
+
17
+ count = 0
18
+ for line in fileinput .input ():
19
+ # split the line into an array of fields
20
+ lst = line .split (" " );
21
+ # if this is the first line, set up the accumulators
22
+ if count == 0 :
23
+ nf = len (lst );
24
+ sum = np .zeros (nf )
25
+ sum2 = np .zeros (nf )
26
+ i = 0
27
+ for x in lst [:] :
28
+ sum [i ] += float (x )
29
+ sum2 [i ] += float (x )* float (x )
30
+ i += 1
31
+
32
+ count += 1
33
+
34
+ for i in range (nf ):
35
+ sum [i ] /= count
36
+ sum2 [i ] = sqrt ((sum2 [i ] - sum [i ]* sum [i ]/ count )/ count )
37
+
38
+ print 'Averages:' ,
39
+ for i in range (nf ):
40
+ print ' {0:.5f}' .format (sum [i ]),
41
+ print
42
+ print 'Std. Devs.:' ,
43
+ for i in range (nf ):
44
+ print ' {0:.5f}' .format (sum2 [i ]),
45
+ print
46
+
47
+
Original file line number Diff line number Diff line change
1
+ 1.234 4.321 8.765
2
+ 2.345 5.432 5.432
3
+ 5.678 8.765 1.090
Original file line number Diff line number Diff line change
1
+ # Python exercises
You can’t perform that action at this time.
0 commit comments