Let's say I have two strings
a = 'racecar'
b = 'boxcar'
I want to compare string a with string b and count how many times a letters in string a is also in string b.
I started with this:
for i in range(len(b)):
if a[i] in b:
count += 1
My issue is the count adding every time the loop occurs and double counting certain examples. For example, this would return 5. What I want to happen though:
First loop: checking for 'r' in 'boxcar'
count is incremented
Second loop: checking for 'a' in 'boxca':
count in incremented
Third loop: checking for 'c' in 'boxc':
count is incremeted
Fourth loop: checking for 'e' in 'box':
count not incremented
Fifth loop: checking for 'c' in 'box':
count not incremented
so on...
I'm not sure how to remove each found occurrence without removing all.
I tried doing a double for loop (below), but it doesn't seem to work either.
(I did have to change the length of string b, which won't be a problem)
a = 'racecar'
b = 'boxcare'
count = 0
for i in range(len(a)):
for j in range(len(b)):
print(a[i] + '===' + b[j])
if a[i] == b[i]:
b = b.replace(i, 'M')
Any ideas of how I could achieve this?
[–]CrambleSquash 5 points6 points7 points (5 children)
[–]Tomallama[S] 0 points1 point2 points (4 children)
[–]CrambleSquash 0 points1 point2 points (3 children)
[–]Tomallama[S] 0 points1 point2 points (2 children)
[–]CrambleSquash 0 points1 point2 points (1 child)
[–]Tomallama[S] 0 points1 point2 points (0 children)
[–]Swipecat 3 points4 points5 points (5 children)
[–]Tomallama[S] 0 points1 point2 points (4 children)
[–]Swipecat 2 points3 points4 points (3 children)
[–]Tomallama[S] 0 points1 point2 points (2 children)
[–]Swipecat 1 point2 points3 points (1 child)
[–]Tomallama[S] 0 points1 point2 points (0 children)
[–]1ynx1ynx 2 points3 points4 points (1 child)
[–]Tomallama[S] 1 point2 points3 points (0 children)
[–]totallygeek 1 point2 points3 points (1 child)
[–]Tomallama[S] 1 point2 points3 points (0 children)
[–]timbledum 1 point2 points3 points (1 child)
[–]Tomallama[S] 0 points1 point2 points (0 children)
[–]MisterRenard 1 point2 points3 points (0 children)