Reputation: 1015
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?
Upvotes: 5
Views: 10974
Reputation: 89547
If you just need to find indexes where letters overlap in Python 3.x you can do it like this:
str_a = "Python is a great language"
str_b = "languages express meaning"
result = [i for i, (a, b) in enumerate(zip(str_a, str_b)) if a == b]
Output
[8, 9, 13, 14, 17, 24]
Upvotes: 0
Reputation: 1095
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.
Upvotes: 3
Reputation: 208465
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
Upvotes: 6
Reputation: 64318
def find_similarity(string_a, string_b):
for ia, ca in enumerate(string_a):
for ib, cb in enumerate(string_b):
if ca == cb:
return ia, ib, ca
If you want all matches, instead of just the first, you can replace the return
statement with a yield
statement, and iterate over the results, or simply:
matches = list(find_similarity(string_a, string_b))
In the latter case, you get:
list(find_similarity(string_a, string_b))
=> [(3, 0, 'h'), (4, 1, 'o'), (5, 2, 'n')]
Upvotes: 2
Reputation: 25096
You should iterate over the indices:
for i in range(len(string_a)):
for j in range(len(string_b)):
if string_a[i] == string_b[j]:
same.append((i, j, string_b[j]))
This will create a list of tuples that look like:
[ (3, 0, "h"), ... ]
Upvotes: 2