原文地址
10行便于理解,结构严密; 如下:
>>> 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)] 本文地址:http://write.blog.csdn.net/postedit/52224806
