Last active
July 5, 2019 14:22
-
-
Save Himan10/80aaaeab504afa0b4d5e22893a246c4e to your computer and use it in GitHub Desktop.
Several methods for removing punctuations from items inside list
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
| import time | |
| import re | |
| import string | |
| pattern = r".*?[\W]+" | |
| # 1 - enumerate and methods | |
| data = [ | |
| "Correct,", | |
| "i", | |
| "loved", | |
| "that", | |
| "part", | |
| '"one', | |
| "has", | |
| "to", | |
| "optimize", | |
| ".....", | |
| '"', | |
| ] | |
| start = time.time() | |
| for i, word in enumerate(data): | |
| data[i] = "".join((char for char in word if not re.search(pattern, char))) | |
| data = [word for word in data if word] | |
| print(data) | |
| end = time.time() | |
| print(end - start) | |
| print() | |
| # 2 - loops and methods | |
| data = [ | |
| "Correct,", | |
| "i", | |
| "loved", | |
| "that", | |
| "part", | |
| '"one', | |
| "has", | |
| "to", | |
| "optimize", | |
| ".....", | |
| '"', | |
| ] | |
| start = time.time() | |
| for i in range(len(data)): | |
| if not data[i].isalpha(): | |
| for j in range(len(data[i])): | |
| if data[i][j] not in string.ascii_letters: | |
| data[i] = data[i].replace(data[i][j], "") | |
| break | |
| data = [word for word in data if word] | |
| end = time.time() | |
| print(data) | |
| print(end - start) | |
| print() | |
| # 3 - regex | |
| data = [ | |
| "Correct,", | |
| "i", | |
| "loved", | |
| "that", | |
| "part", | |
| '"one', | |
| "has", | |
| "to", | |
| "optimize", | |
| ".....", | |
| '"', | |
| ] | |
| start = time.time() | |
| new_data = [re.sub(r"[\W]", "", i) for i in data] | |
| new_data = [word for word in new_data if word] | |
| print(new_data) | |
| end = time.time() | |
| print(end - start, '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment