Just some additional explanation, if the text file reads:
12, 31, 12
5, 3, 12
32, 12, 2
My program currently will output:
[12, 31, 12, 55, 18.33]
[5, 3, 12, 20, 6.67]
[32, 12, 2, 46, 15.33]
[49, 46, 26]
I need another row underneath all of that contains the averages of each column.
# Python 3.1
import sys
def split(l):
r = []
n = l.count(',')
for i in range(n):
r = r + [int(l[0:l.index(',')])] #convert text to an integer
l = l[l.index(',') +1:len(l)] # remove text
# on next iteration, next value will be considered
r = r + [int(l)] # last element
return r
f = sys.stdin
fn = "C:\\Users\Elisha\Documents\CS1MA3\Python\E3.txt"
f = open(fn,'r')
l = f.readline()
if l[len(l)-1] == '\n':
l = l[0:len(l)-1]
x = []
while l != '':
x = x + [l]
l = f.readline()
if l != '' and l[len(l)-1] == '\n':
l = l[0:len(l) - 1]
f.close()
t = []
for i in x:
new = split(i)
t.append(new)
total = sum(split(i))
average = round(total/len(new), 2)
print(split(i) + [total] + [average])
result = [0] * len(t)
for c in range(len(t[0])):
for r in range(len(t)):
result[c] += t[r][c]
print(result)
Thanks!

New Topic/Question
Reply




MultiQuote




|