سؤال

Lets say I'm building a rudimentary search engine of sorts. I have a list of strings as the search results, and I want to order the list of search results with the best matching results at the top.

My current code looks like this (named parameters as examples)

import difflib
def order_by_best_match(search_results=["spam", "eggs", "spammy", "eggy"], search_query="spam"):

    for result in search_results:
        ratio = difflib.SequenceMatcher(None, result, search_query).ratio()

I don't know what to do with ratio after that. I know I have to sort the list by ratio, but how would I do that?

هل كانت مفيدة؟

المحلول

>>> import difflib
>>> a = ["spam", "eggs", "spammy", "eggy"]
>>> b = 'spam'
>>> sorted(a, key=lambda x: difflib.SequenceMatcher(None, x, b).ratio())
['eggy', 'eggs', 'spammy', 'spam']

Also, if you want the reverse order:

>>> sorted(a, key=lambda x: difflib.SequenceMatcher(None, x, b).ratio(), reverse=True)
['spam', 'spammy', 'eggs', 'eggy']

نصائح أخرى

The sorted function takes a key parameter, which you can use to determine how things are ranked. A common practice is to build a list of tuples, and then sort based on one element of the tuple.

for result in search_results:
    ratio = difflib.SequenceMatcher(None, result, search_query).ratio()
    weighted_results.append((result, ratio))

print weighted_results
print sorted(weighted_results, key=lambda x: x[1])

gives us

[('spam', 1.0), ('eggs', 0.25), ('spammy', 0.8), ('eggy', 0.0)]
[('eggy', 0.0), ('eggs', 0.25), ('spammy', 0.8), ('spam', 1.0)]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top