Skip to content

Instantly share code, notes, and snippets.

@pelid
Created November 3, 2017 14:49
Show Gist options
  • Save pelid/fc74095b3b1e9370101650801664dff1 to your computer and use it in GitHub Desktop.
Save pelid/fc74095b3b1e9370101650801664dff1 to your computer and use it in GitHub Desktop.
import string
import re
import timeit
def remove_unwanted_symbols_1(text):
test_list = []
for symbol in text.lower():
if symbol in string.ascii_letters:
test_list.append(symbol)
return ''.join(test_list)
def remove_unwanted_symbols_2(text):
test_list = []
for symbol in text.lower():
if symbol == "-" or symbol == "`":
continue
test_list.append(symbol)
return ''.join(test_list)
def remove_unwanted_symbols_3(text):
test_list = []
for symbol in text.lower():
if symbol.isalpha():
continue
test_list.append(symbol)
return ''.join(test_list)
if __name__ == '__main__':
with open('lorem.txt') as f:
text = f.read()
print('Letters count is', len(text))
print(timeit.Timer(lambda: remove_unwanted_symbols_1(text)).timeit(number=5))
print(timeit.Timer(lambda: remove_unwanted_symbols_2(text)).timeit(number=5))
print(timeit.Timer(lambda: remove_unwanted_symbols_3(text)).timeit(number=5))
@pelid
Copy link
Author

pelid commented Nov 3, 2017

Результат вычислений на моей машине:

$ python3 test.py 
Letters count is 28367
0.03142735400251695
0.030193180002243025
0.02767529800257762

Вывод: дело не в isalpha

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