I have a text file that looks like this; the values are tab separated:
diamond orange pear loc1 . + 0.0 0.0 0.0 0.0 1.0 1.2 3.4
diamond orange pear loc2 . + 1.0 0.0 0.0 0.0 1.0 1.2 2.3
diamond orange pear loc3 . + 2.0 0.0 3.0 0.0 0.0 0.0 1.4
# ......
For each line in the file I want to make a ratio of the sum of the first 3 values divided by the sum of the last 4 values. The output would look like:
diamond orange pear loc1 . + 0
diamond orange pear loc2 . + 0.22
diamond orange pear loc3 . + 4.28
......
I would like to do this in python.
with open('/path/to/file/') as inFile:
inFile.next()
for line in inFile:
data = cols[6:]
data = map(float,data)
sum_3 = [sum[for x in x data[0:3]]
sum_last = [sum[for x in x data[4:7]]
average = sum_3/sum_last
This doesn't work, and I was hoping if I could get some advice?
sumfunction using square brackets?sumsyntax is all wrong.[sum[for x in x data[0:3]]should besum(x for x in data[0:3]), or rathersum(float(x) for x in data[0:3])datais the result of amap(float, ...)call, I don't think the additionalfloat()conversion is necessary there. :-P