Python: how to sort a list of strings by substring relevance?

后端 未结 3 455
[愿得一人]
[愿得一人] 2021-01-16 06:38

I have some list of strings, for example:

["foo bar SOME baz TEXT bob",
"SOME foo bar baz bob TEXT",
"SOME foo TEXT",
"         


        
3条回答
  •  星月不相逢
    2021-01-16 07:09

    You can use difflib.SequenceMatcher, to achieve something very similar to your desired output:

    >>> import difflib
    >>> l = ["foo bar SOME baz TEXT bob", "SOME foo bar baz bob TEXT", "SOME foo TEXT", "foo bar SOME TEXT baz", "SOME TEXT"]
    >>> sorted(l, key=lambda z: difflib.SequenceMatcher(None, z, "SOME TEXT").ratio(), reverse=True)
    ['SOME TEXT', 'SOME foo TEXT', 'foo bar SOME TEXT baz', 'foo bar SOME baz TEXT bob', 'SOME foo bar baz bob TEXT']
    

    If you can't tell the only difference is that the position of the two elements "foo bar SOME TEXT baz" and "SOME foo TEXT" are swapped compared to your desired output.

提交回复
热议问题