0

Now and then I'll write out a literal dictionary and then realize I inadvertently swapped the keys/values:

MY_DICT = {"VALUE1":"KEY1", "VALUE2":"KEY2"} # <- WHOOPS!

Sometimes such a dictionary can be a bit long, and it is a lot of typing to go back and swap all the keys/values.

One way to fix this is to copy the dict to the interpreter and do a:

{v:k for k,v in MY_DICT.values()}

...and copy/paste the result. But of course, this is also a lot of typing.

The following regex in the Find field would capture key/value group pairs for a dictionary with 6 entries:

\{(.*): (.*), (.*): (.*), (.*): (.*), (.*): (.*), (.*): (.*), (.*): (.*)\}

...I can then swap them like this in the Replace field:

{$2: $1, $4: $3, $6: $5, $8: $7, $10: $9, $12: $11}

Is there a more generic way to execute this find/replace? If I can come up with something, I was thinking of adding a custom SwapDict command to my PyCharm IDE.

(Complicating things, of course, is we probably cannot count on having nice single-spaces after every colon and comma. And the keys/values might also contain colons sometimes! This is the real world, after all.)

Maybe another approach would be to abandon the regex entirely and teach PyCharm a new Alt+Enter trick: take the dict, swap the keys/values, and replace the literal dict with the result. I have no idea how to do this.

Rick supports Monica
  • 33,838
  • 9
  • 54
  • 100
  • Couldn't you do this `re.sub(r'("[^"]+")(:\s*)("[^"]+")', "\\3\\2\\1", str, 0)`? – revo Mar 27 '18 at 20:06
  • can you assume that there are no strings with curly braces? are all keys and values strings? – e.s. Mar 27 '18 at 20:07
  • @e.s. No, not all are strings. Some might be object names. I suppose a string could have braces. – Rick supports Monica Mar 27 '18 at 20:09
  • Could be wrong but this might be out of the reach of regex- regex can't parse HTML because of the matching tag pairs, and similarly you can't even write a regex that matches any other regex. This might be a similar kind of problem? – Rick supports Monica Mar 27 '18 at 20:12
  • Regex can't parse HTML because it has a tree-structure. Fallen in a top-down hierarchy which naturally is able to be nested. If you reply on my first comment (about possibilities) I might be able to help. – revo Mar 27 '18 at 20:14
  • If you need to do that in a text editor, I'd recommend Notepad++ where you may use [`(?:\G(?!\A)\s*,\s*|\bMY_DICT\s*=\s*\{)\K("[^"]+")(\s*:\s*)("[^"]+")` to replace with `$3$2$1`](https://regex101.com/r/PqewFk/1). – Wiktor Stribiżew Mar 27 '18 at 20:19
  • @WiktorStribiżew Pretty nice (I also am a heavy Npp++ user). However the regex doesn't work for non-string object references, e.g. `{VALUE_OBJ1:"KEY1", VALUE_OBJ2:"KEY2"}`. – Rick supports Monica Mar 27 '18 at 20:53
  • To slightly adapt the above for use with Pycharm: [`(\G(?!\A)\s*,\s*|\bMY_DICT\s*=\s*\{)(\"[^\"]+\")(:\s*)(\"[^\"]+\")`](https://regex101.com/r/aMciMD/2) – user3483203 Mar 27 '18 at 20:55
  • Like https://regex101.com/r/PqewFk/2? – Wiktor Stribiżew Mar 28 '18 at 10:58

0 Answers0