Last active
August 29, 2015 14:23
-
-
Save amjith/cf79e9a66d9867a1fee4 to your computer and use it in GitHub Desktop.
This file contains 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 re # regex module from standard library. | |
>>> def fuzzyfinder(user_input, collection): | |
suggestions = [] | |
pattern = '.*'.join(user_input) # Converts 'djm' to 'd.*j.*m' | |
regex = re.compile(pattern) # Compiles a regex. | |
for item in collection: | |
match = regex.search(item) # Checks if the current item matches the regex. | |
if match: | |
suggestions.append(item) | |
return suggestions | |
>>> print fuzzyfinder('djm', collection) | |
['django_migrations.py', 'django_admin_log.py'] | |
>>> print fuzzyfinder('mig', collection) | |
['django_migrations.py', 'django_admin_log.py', 'main_generator.py', 'migrations.py'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment