-3

I need your help by sorting a text file with the following input:


id scalar2_q1p2_114(-2,-1) = 
 + 0
;
id scalar2_q1p2_114(-2,-2) = 
 + 0
;
id scalar2_q1p2_114(-2,0) = 
 + 0
;

id scalar2_q1p2_114(-2,1) = 
 + 0
;

id scalar2_q1p2_114(-2,2) = 
 + 0
;

id scalar2_q1p2_114(-1,-2) = 
 + 0
;

id scalar2_q1p2_114(-1,-1) = 
 + 0
;

id scalar2_q1p2_114(-1,0) = 
 + 0
;

id scalar2_q1p2_114(-1,1) = 
 + 0
;

id scalar2_q1p2_114(-1,2) = 
 + 0
;

id scalar2_q1p2_114(0,-2) = 
 + 0
;

id scalar2_q1p2_114(0,-1) = 
 + 0
;

id scalar2_q1p2_114(0,0) = 
 + 0
;

id scalar2_q1p2_114(0,1) = 
 + 0
;

id scalar2_q1p2_114(0,2) = 
 + 0
;

id scalar2_q1p2_114(1,-2) = 
 + 0
;

id scalar2_q1p2_114(1,-1) = 
 + 0
;

id scalar2_q1p2_114(1,0) = 
 + 0
;

id scalar2_q1p2_114(2,-2) = 
 + 0
;

id scalar2_q1p2_114(2,-1) = 
 + 0
;

id scalar2_q1p2_114(2,0) = 
 + 0
;

id scalar2_q1p2_114(1,2) = 
 + scalar2_q1p2_114(1,1)*((-d+3)*den(p22))
;

id scalar2_q1p2_114(2,1) = 
 + scalar2_q1p2_114(1,1)*((-d+3)*den(p22))
;

id scalar2_q1p2_114(2,2) = 
 + scalar2_q1p2_114(1,1)*((d^2-9*d+18)*den(p22^2))
;

I want to open the text file and order the argument by the rule (-2,-2)->(-2,-1)->(-2,0)...(2,2)

For this example the first element and second element should be swap but i want a general programm to do this for any position of the arguments.

Can somebody help me out with this.

It is similar to this question: Alphanumeric sorting in Python and negative numbers

But only that my string is a little more complicated

NeAr
  • 137
  • 3
  • TBH, your question doesn't make much sense to me. Can you clarify? – Brian Feb 27 '20 at 18:53
  • What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service. See: [tour], [ask], [help/on-topic], https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – AMC Feb 27 '20 at 18:57
  • What is the question you are asking? Do you have a specific issue? If you are simply asking for code to _do it for you_ that is not what SO is for. – Object object Feb 27 '20 at 19:01
  • Sorry for the confusion. My problem is to sort the text file by lowest to highest number. What makes it a litlle harder is that the numbers e.g. (-2,-2) are embedded with a string. I have no clue how to do sort the text file in a elegant and general way. Unfortunately I cannot give you a good context to this text file. It is the output of another software related to physics – NeAr Feb 27 '20 at 19:32

1 Answers1

0

You can create a function which uses regular expressions to parse out the numbers you want to isolate and output the number so that you can use the function as the key in a sort() of the lines in the program. So something like

import re
with open('your_file.txt','r') as f:
    txt = f.read()
    txt_list = re.split('\n;\n',txt)
    sorted_lines = txt_list.sort(key=your_regex_function_here)

I'd suggest reading through the documentation I've linked above for more information on how to use these features of Python.

Rice
  • 17
  • 1
  • 8