python check string contains all characters

后端 未结 4 1626
粉色の甜心
粉色の甜心 2021-01-29 11:10

I\'m reading in a long list of words, and I made a node for every word in the list. Each node has an attribute \'word\' for their position in the list.

I am trying to co

4条回答
  •  盖世英雄少女心
    2021-01-29 12:01

    You can use the 3rd party python library, python-levenshtein, to calculate the Levenshtein Distance which is the string edit distance. In your case, the only allowed 'edit' is the 'insertion' of the character on the next string/word on your list, so you will also need to verify that the length of the next word is 1 plus the previous word.

    Here is the sample code that would achieve our stuff:

    import Levenshtein as lvst
    
    if len(word2) - len(word1) == 1 and lvst.distance(word1, word2) == 1:
        print(word1, word2)
    

    You can install python-levenshtein by either apt-get (systemwide) or pip:

    sudo apt-get install python-levenshtein

    or

    sudo apt-get install python3-levenshtein

    or

    pip install python-levenshtein

提交回复
热议问题