Here's an alternative if you prefer speed over elegance (without recursion).
This version assumes that you flatten in order to print.
If that is not the case, you may leave out the "[“+... and ...+“]" part before returning the result and apply .split(",") instead, so the last line of flatten should read:
return re.sub("[ \[\]\n]+", "", s).split(",")
This version assumes that you flatten in order to print.
If that is not the case, you may leave out the "[“+... and ...+“]" part before returning the result and apply .split(",") instead, so the last line of flatten should read:
return re.sub("[ \[\]\n]+", "", s).split(",")
Data Science Cheat Sheet - Python Regular Expressions.pdf
49 KB
Cheatsheet of Python Regular Expressions
Regular expressions are a powerful language for matching text patterns. A special sequence of characters helps us match or find other strings or sets of strings, using a specialized syntax held in a pattern. The power of python regular expressions is that they can specify patterns, not just fixed characters.
Credit: Dataquest
Regular expressions are a powerful language for matching text patterns. A special sequence of characters helps us match or find other strings or sets of strings, using a specialized syntax held in a pattern. The power of python regular expressions is that they can specify patterns, not just fixed characters.
Credit: Dataquest
#Python default argument values are evaluated once so mutability can bite you, use "None" instead.