Skip to content

Instantly share code, notes, and snippets.

@carlsmith
Created April 5, 2017 02:08
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save carlsmith/b2e6ba538ca6f58689b4c18f46fef11c to your computer and use it in GitHub Desktop.
Save carlsmith/b2e6ba538ca6f58689b4c18f46fef11c to your computer and use it in GitHub Desktop.
A Python function that does multiple string replace ops in a single pass.
import re
def replace(string, substitutions):
substrings = sorted(substitutions, key=len, reverse=True)
regex = re.compile('|'.join(map(re.escape, substrings)))
return regex.sub(lambda match: substitutions[match.group(0)], string)
@dgr113
Copy link

dgr113 commented Sep 22, 2020

You have your point of view, and I have my...
The principles of code readability are important to me.
I try to use lambda functions as little as possible, because they degrade performance and do not support typing.
In addition, I prefer to immediately make the functionality more flexible for adding new features in the future. It seems to me that it is more convenient to add a new methods or protocols than to rewrite everything.
I myself don't really like to use OOP unnecessarily, but I think the principle of encapsulation (in methods for example) is the most important.

PS: I only suggested my version, including your solution. With hope that this will help someone, according to OpenSource spirit.
I'm not attacking your programming principles =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment