python模糊查询

import re

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((len(match.group()), match.start(), item))
    return [x for _, _, x in sorted(suggestions)]

if __name__ == '__main__':
    collection=['ls','ll','ls -a','ll -a']
    fuzzyfinder('user', collection)

 

你可能感兴趣的:(Python,python模糊查询,python,python快速查找,python前端快速查找)