0

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 = 0
  • output.write(name0,bccords0*AT)
  • counting = 1
  • output.write(name1,bccords1*AT)
  • counting = 2
  • output.write(name2,bccords2*AT)
  • counting = 3
  • output.write(name3,bccords3*AT)
  • counting = 4
  • (counting > 3) is true
  • output.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()

Cœur
  • 37,241
  • 25
  • 195
  • 267
adrienlucca.net
  • 677
  • 2
  • 10
  • 26

1 Answers1

0

Sorry for this question, it was easy, I found my solution there:

python store variable in function and use it later

If any help, here's my new 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], 'none', '0', 'none', '0', 'none', '0', 'none', '0')
    counting = 0
    top = 0

    for i, name in sorted(enumerate(names), key=lambda (i, name): priority_list[name]):
        VAR1 = (bcoords[i]*AT)
        VAR2 = int(((bcoords[i]*AT)/(1-top))*65535)
        output.write(',%s,%s' % (name, VAR2))
        counting = counting + 1
        top = top + VAR1

        if counting > 3:
            output.write('\n')
            counting = 0
Community
  • 1
  • 1
adrienlucca.net
  • 677
  • 2
  • 10
  • 26