Created
January 10, 2012 22:40
-
-
Save matiskay/1591655 to your computer and use it in GitHub Desktop.
Refactoring the clean function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# After | |
def clean(i_list): | |
""" | |
Generate a string from a list stripped all the extra whitespace | |
""" | |
r_list = [string.strip() for string in i_list] | |
return ' '.join(r_list) | |
# Before | |
def clean(i_list): | |
r_list = [] | |
for x in i_list: | |
pattern = re.compile(r'\s+') | |
x = re.sub(pattern, ' ', x) | |
x = x.strip() | |
r_list.append(x) | |
return ' '.join(r_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment