1

I have data result from this code

import os
with open('c.txt', 'r', encoding='utf-8') as f1, open('c.txt', 'r', encoding='utf-8') as f2, open('r.txt', 'w', encoding='utf-8') as outfile:

    for i, line in enumerate(f1):
        xi, yi = (float(n) for n in line.split())
        f2.seek(0)
        for j, line in enumerate(f2):
            if i < j:
                xj, yj = (float(n) for n in line.split())
                print( ((xi-xj)**2-(yi-yj)**2), file=outfile)
                
        print(((xi)**2-(yi)**2), file=outfile) 

this code should give me the calculations but need to print the position of x and y every time it is calculated to be like this

1 2 0.5
1 3 0.55
1 4 2.02

and so on. I tried to write the code but couldn't merge the concept I want. I think I need to write this code to get the position of the value and print it

 for pos in range(len(f1)):
        cor = t[pos]
        f.write(str(pos) + ' ' + str(cor[0]) + ' ' + values + '\n')

but coluldn't merge it

sample of c.txt like

9.2    2.02
-2.3   1.5
user1
  • 501
  • 2
  • 9
  • 24
  • Does this answer your question? [Print multiple arguments in Python](https://stackoverflow.com/questions/15286401/print-multiple-arguments-in-python) – yudhiesh Feb 02 '21 at 05:12
  • no it's different in the logic i need . thanks – user1 Feb 02 '21 at 05:13
  • Can you share sample data from `c.txt` ? – Joe Ferndz Feb 02 '21 at 05:14
  • What is the current output? – yudhiesh Feb 02 '21 at 05:14
  • @yudhiesh I couldn't merge the concept I need with my code – user1 Feb 02 '21 at 05:14
  • @JoeFerndz please see the edit post – user1 Feb 02 '21 at 05:15
  • Append the numbers to a string and add the spaces then print – Lightning Gaming Feb 02 '21 at 05:15
  • @LightningGaming you mean like this print(str(i)+ ' ' + str(j) +' ' +((xi-xj)**2-(yi-yj)**2), file=outfile) but I tired it and didn't work as it will print the number it self and I need to print the position of the number – user1 Feb 02 '21 at 05:16
  • I think you can optimize your code a bit. You are reading the full file (f2 - c.txt) for every row read in file 1 (f1 - c.txt). Why dont you just store the data into an array and then process it. Reading from file is slower and costlier. – Joe Ferndz Feb 02 '21 at 05:18
  • @JoeFerndz can you give me an example please ? – user1 Feb 02 '21 at 05:19
  • Let's say there are 5 rows in c.txt. When you read line 1 in f1, you are reading f2 5 times, then when you read line 2 in f1, you reading f2 5 times again. And it goes on. If you read c.txt and store it once, then you can just use that list as many times as you want. Helps? – Joe Ferndz Feb 02 '21 at 05:21
  • ok, but i don't know how can I got the first value with the second value and do an increment to give me the rest with the logic I want. I tried that logic before but got errors .. if you please can you write this to see it . thanks in advance – user1 Feb 02 '21 at 05:27
  • See the code and let me know if you need additional info. By the way, your value of `1 2 0.5` is not what i got with your formula. – Joe Ferndz Feb 02 '21 at 05:58

1 Answers1

2

Let's see if the answer helps you understand what I was trying to explain in the comments section.

  • Step 1: Open one file to read, one file to write
  • Step 2: Read all the values from file 1 and create a list of list. Each list will have two values x and y
  • Step 3: Iterate through the list using enumerate getting values of xi and yi where i is the row being selected
  • Step 4: Iterate through the list (remaining items using [i+1:]) using enumerate
  • Step 5: Do the calculation and print the data into the file

Note that every time you enumerate, the count starts with 0. So you need to add 1 to i and 1 to j. So when you print i, you use +1, when you print j, you use +2

with open("abc.txt", "r") as f_in, open("out.txt", "w") as f_out:
    abc_list = [[float(n) for n in line.split()] for line in f_in]
    print (f' i\t j\txi\tyi\txj\tyj\t(xi-xj)**2-(yi-yj)**2')    
    for i,(xi,yi) in enumerate(abc_list):
        for j,(xj,yj) in enumerate(abc_list[i+1:]):
            print (f'{i+1:2}\t{i+j+2:2}\t{xi:.2}\t{yi:.2}\t{xj:.2}\t{yj:.2}\t{(xi-xj)**2-(yi-yj)**2:.2f}')
            f_out.write(f'{i+1:2}\t{i+j+2:2}\t{xi:.2}\t{yi:.2}\t{xj:.2}\t{yj:.2}\t{(xi-xj)**2-(yi-yj)**2:.2f}\n')

Your input file data:

9.2    2.02
-2.3   1.5
3.2    4.5
1.3    2.4

Your output file data:

 1   2  9.2     2.0    -2.3  1.5    131.98
 1   3  9.2     2.0    3.2   4.5    29.85
 1   4  9.2     2.0    1.3   2.4    62.27
 2   3  -2.3    1.5    3.2   4.5    21.25
 2   4  -2.3    1.5    1.3   2.4    12.15
 3   4  3.2     4.5    1.3   2.4    -0.80

The data printed is:

 i   j  xi      yi    xj    yj    (xi-xj)**2-(yi-yj)**2
 1   2  9.2     2.0   -2.3  1.5   131.98
 1   3  9.2     2.0   3.2   4.5   29.85
 1   4  9.2     2.0   1.3   2.4   62.27
 2   3  -2.3    1.5   3.2   4.5   21.25
 2   4  -2.3    1.5   1.3   2.4   12.15
 3   4  3.2     4.5   1.3   2.4   -0.80

Use this instead:

    for j,(xj,yj) in enumerate(abc_list[i+1:]):
        i_val = i+1
        j_val = i+j+2
        result = (xi-xj)**2-(yi-yj)**2
        
        print (f'{i_val:2}\t{j_val:2}\t{xi:.2}\t{yi:.2}\t{xj:.2}\t{yj:.2}\t{result:.2f}')
        f_out.write(f'{i_val:2}\t{j_val:2}\t{xi:.2}\t{yi:.2}\t{xj:.2}\t{yj:.2}\t{result:.2f}\n')
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • thanks but if i need to change the equation because I have another problem like it with another file which is math.sqrt((xi-xj)**2-(yi-yj)**2) I tired to use your code with it but got SyntaxError: f-string expression part cannot include a backslash i wrote like f_out.write(f'{i+1:2}\t{i+j+2:2}\t{math.sqrt((xi-xj)**2-(yi-yj)**2:.2f}\n') – user1 Feb 02 '21 at 06:13
  • instead of doing all the computation in the f'string, try to do the computation one line above and just use the variable in the fstring – Joe Ferndz Feb 02 '21 at 06:15
  • 1
    i fixed it .. i missed one of the Parentheses – user1 Feb 02 '21 at 06:17
  • thanks for helping .. finally its done .. I hope you can help me with further problems :-) if you please can I get your email ? – user1 Feb 02 '21 at 06:19
  • 1
    Post your question on Stack Overflow and I will be able to help. Alternate, you can chat with me on https://chat.stackoverflow.com/users/13873980/joe-ferndz – Joe Ferndz Feb 02 '21 at 06:22
  • can you help https://stackoverflow.com/questions/66075524/how-to-avoid-out-of-memory-python ? – user1 Feb 06 '21 at 09:54