Reputation: 111
I am trying to calculate the distance between two strings. The distance/difference between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other.
The method I have tried is to: convert two strings into lists, compare lists, check the differences, then add the differences
first_string = "kitten"
second_string = "sitting"
list_1 = list(first_string)
list_2 = list(second_string)
print("list_1 = ", list_1)
print("list_2 = ", list_2)
print(" ")
lengths = len(list_2) - len(list_1)
new_list = set(list_1) - set(list_2)
print(lengths)
print(new_list)
difference = lengths + int(new_list)
print(difference)
the output I get is:
list_1 = ['k', 'i', 't', 't', 'e', 'n']
list_2 = ['s', 'i', 't', 't', 'i', 'n', 'g']
1
{'e', 'k'}
Of which then I am trying to find out how to add these differences so it equals 3. I don't know how to make the outputs similar to add them together (adding 1 with {'e', 'k'} to equal a distance of 3).
Upvotes: 3
Views: 1753
Reputation: 21
This is referred to as the Levenshtein distance. Check out this implementation as further reading.
Upvotes: 2
Reputation: 66
You're almost there. Calculate the length of new_list using len() like you did with lengths:
difference = lengths + len(new_list)
Upvotes: 3
Reputation: 1299
Looks like you just need to change this line:
difference = lengths + int(len(new_list))
That should give you 3 like you want :)
Upvotes: 2