unter_983
unter_983

Reputation: 155

How to extract the difference given by the Levensthein distance?

The Levensthein distance provides a number which is the total number of differences between two strings. My question is: is it possible to retrieve, instead of the number, what are these differences? For example

a = "MyTest"
b = "MynewTest"

The Levensthein will be 3, but how do I retrieve and store the string "new"? I might combine the ndiff library but is there an already available function?

Upvotes: 1

Views: 113

Answers (1)

U13-Forward
U13-Forward

Reputation: 71610

Try this:

import difflib
print(''.join([i[-1] for i in difflib.ndiff("MyTest", "MynewTest") if i[0] == '+']))

Output:

new

Upvotes: 1

Related Questions