0

I write the solution of my code on a file and I need to read it for another code. But there is one problem , in the file which saves the solution , the variables are like:

x[1][1]=3 x[1][2]=5 x[1][3]=9 x[2][1]=3 x[2][2]=5 x[2][3]=9

what I need is :

x[1][1]=3 x[2][1]=5 x[3][1]=9 x[1][2]=3 x[2][2]=5 x[3][2]=9

how can I flip the for loop to change the indexes to the other way? I learnt that I can make regex on python but since I could not find a good example of it, I could not apply it. Can you give me an example for how to do it ? Thank you so much

1
  • show us the code that stores this and fix that ... Commented Oct 21, 2018 at 19:03

2 Answers 2

2

Read it as normal, then transpose.

You can transpose like this :

     x = list(map(list, zip(*x)))

Transpose "flips" the indices in the table.

Sign up to request clarification or add additional context in comments.

Comments

2

Actually transposing the list per Christian Sloper's suggestion is probably the most principled thing to do, but if you truly want to use regex to transform the string "list[x][y]" into "list[y][x]", that's very doable:

import re
input_string = "list[3][4], list[7][8]"
pattern = r"\[(\d+)\]\[(\d+)\]"
repl = r"[\2][\1]"
re.sub(pattern, repl, input_string) #list[4][3], list[8][7]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.