I am beginner at python but wanting to learn from the problems I encounter.
At the end of a wroking script I have this code (see below) that outputs in a .txt file a series of values:
name, bcoords[i]*AT.
The operation is repeated 4 times, until counting > 3 is true
what happens is this (pseudocode):
open('solution_AT.txt','a')output.write(str(target[0]))output.write('\n')counting = 0output.write(name0,bccords0*AT)counting = 1output.write(name1,bccords1*AT)counting = 2output.write(name2,bccords2*AT)counting = 3output.write(name3,bccords3*AT)counting = 4(counting > 3)istrueoutput.write('\n')counting = 0
Instead of this, I would like to temporarily keep the values:
bcoords0*AT, bcoords1*AT, bcoords2*AT, bcoords3*AT
Because I want to perform some variable-interdependent mathematical operations on them.
Namely, what I want to do is this (pseudocode):
first, perform math operations on variables bcoords
a = bcoords0*AT / (1-bcoords3*AT-bcoords2*AT-bcoords1*AT)b = bcoords1*AT / (1-bcoords3*AT-bcoords2*AT)c = bcoords2*AT / (1-bcoords3*AT)d = bcoords3*AT
second, write the values in the same order as the code below, but with: a, b, c, d instead of bcoords0*AT, bcoords1*AT, bcoords2*AT, bcoords3*AT
open('solution_AT.txt','a')output.write(str(target[0]))output.write('\n')output.write(name0,a)output.write(name1,b)output.write(name2,c)output.write(name3,d)output.write('\n')
A line of this output would have the structure:
target[0], name0, a, name1, b, name2, c, name3, d \n
And... I don't find how to do that, any help is much appreciated! Best
Code:
output = open('solution_AT.txt','a')
if tet_i == None:
output.write(str(target[0]))
output.write('\n')
else:
names = [colors[i][0] for i in tg.tets[tet_i]]
sorted_indices = sorted(enumerate(names), key=lambda (i, name): priority_list[name])
output.write(target[0])
counting = 0
for i, name in sorted(enumerate(names), key=lambda (i, name): priority_list[name]):
output.write(',%s,%s' % (name, bcoords[i]*AT))
counting = counting + 1
if counting > 3:
output.write('\n')
counting = 0
output.close()