Welcome, guest | Sign In | My Account | Store | Cart
Python, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#this one liner will add each column
#and return the added columns as list

#adds all columns
addCol1 = lambda lines:apply(map,(lambda *args:reduce(lambda a,v:(a and float(a) or 0)+(v and float(v) or 0),args,0),)+tuple(map(lambda l:l.split(),lines)))

#adds only those columns which are complete
addCol2 = lambda lines:[reduce(lambda a,v:float(a)+float(v),col,0) for col in apply(zip,tuple([l.split() for l in lines]))]

#simulating... lines = open(file,'r').readlines()
lines = ['1 2 3 4',
         '3 4 5',
         '5 6 7',
         '8 0 9']

print addCol1(lines)
print addCol2(lines)
Created by anurag uniyal on Wed, 10 Apr 2002 (PSF)
Python recipes (4591)
anurag uniyal's recipes (12)

Required Modules

  • (none specified)

Other Information and Tasks