I have a user inputting two strings and then I want to check if there are any similar characters and if there is, get the position where the first similarity occurs, without using the find or index function.
Below is what I have so far but I doesn't fully work. With what I have so far, I'm able to find the similarities but Im not sure how to find the position of those similarities without using the index function.
string_a = "python"
string_b = "honbe"
same = []
a_len = len(string_a)
b_len = len(string_b)
for a in string_a:
for b in string_b:
if a == b:
same.append(b)
print (same)
Right now the output is:
['h', 'o', 'n']
So basically what I am asking is, how can I find the position of those characters without using the Python Index function?
This is a perfect use case for difflib.SequenceMatcher
:
import difflib
string_a = 'python'
string_b = 'honbe'
matcher = difflib.SequenceMatcher(a=string_a, b=string_b)
match = matcher.find_longest_match(0, len(matcher.a), 0, len(matcher.b))
The match
object will have the attributes a
, b
, and size
, where a
is the starting index from the string matcher.a
, b
is the starting index from matcher.b
, and size
is the length of the match.
For example:
>>> match
Match(a=3, b=0, size=3)
>>> matcher.a[match.a:match.a+match.size]
'hon'
>>> match.a
3
>>> match.b
0
You can solve this problem using a combination of list comprehensions and itertools.
import itertools
string_a = 'hello_world'
string_b = 'hi_low_old'
same = [ i for i,x in enumerate(itertools.izip(string_a,string_b)) if all(y==x[0] for y in x)]
In [38]: same
Out[38]: [0, 3, 4, 7]
Here we compare the two strings element by element and return all the indexes that have been found to be similar. The output can be easily changed to include the characters that matched etc. This method scales easily to compare multiple words.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With