strcmp for python or how to sort substrings efficiently (without copy) when building a suffix array

后端 未结 4 1751
孤独总比滥情好
孤独总比滥情好 2021-02-06 00:24

Here\'s a very simple way to build an suffix array from a string in python:

def sort_offsets(a, b):
    return cmp(content[a:], content[b:])

content = \"foobar          


        
4条回答
  •  遇见更好的自我
    2021-02-06 01:14

    I don't know if there's a fast way to compare substrings, but you can make your code much faster (and simpler) by using key instead of cmp:

    suffix_array.sort(key=lambda a: content[a:])
    

    This will create the substring just once for each value of a.

    Edit: A possible downside is that it will require O(n^2) memory for the substrings.

提交回复
热议问题