I’m looking for a pythonic and efficient replacement for the following self-explanatory code:
term = "< << >" # turn this into "( < )"
term.replace("<<", "#~").replace(">>", "~#").replace(">", ")").replace("<", "(").replace("#~", "<").replace("~#", ">")
Any ideas?
Here’s a shorter method than my first answer. It splits the input on the doubled-up character sequence to remove them, then joins those segments back up again with the replacement single character. As before it uses a dictionary to specify the replacements that should be made.