2

I'm trying to compare the characters from 2 separate strings, the idea is that i will return a value corresponding to how many characters the strings both share. for example, if string one was 'mouse' and string 2 was 'house'. They would share 4/5 characters. its important to note that they only share a character if it is in the same 'index position'

def compareWords(word1, word2):
    result = 0
    if word1[0] in word2[0]:
        result += 1
    if word1[1] in word2[1]:
        result += 1
    if word1[2] in word2[2]:
        result += 1
    if word1[3] in word2[3]:
        result += 1
    if word1[4] in word2[4]:
        result += 1
    if word1[5] in word2[5]:
        result += 1
        print result, '/5'

working result

non-working result

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
RhysBuddy
  • 125
  • 3
  • 11
  • If you compare two strings to be equal you should use `==` and not `in`. – Matthias Apr 06 '16 at 15:56
  • already tryed that still get the same issue – RhysBuddy Apr 06 '16 at 15:57
  • Well, my comment was not the solution, but more of a "how to". Nevertheless your code should work if you fix the indentation and the parts that don't belong there (the "enter image description" stuff). But if you want a short and flexible solution use Padraic Cunninghams code. – Matthias Apr 06 '16 at 16:01
  • 1
    the indentation of "print result" once altered fixed the problem, thank you. apologies for my lack of knowledge i decided to go to python without actually taking the time to learn the basics – RhysBuddy Apr 06 '16 at 16:08

2 Answers2

4

zip and sum:

a,b = "house", "mouse"

print(sum(s1 == s2 for s1, s2 in zip(a, b)))
4

zipping will pair the characters at the same index, then summing how many times s1 == s2 will give you the count of matching chars:

In [1]: a,b = "house", "mouse"

In [2]: zip(a, b)
Out[2]: [('h', 'm'), ('o', 'o'), ('u', 'u'), ('s', 's'), ('e', 'e')]

The only thing that is unclear is what you use as the out of if the strings are of different lengths.

If you did want the matches and the sum you can still use the same logic:

def paired(s1, s2):
    sm, index_ch = 0, []
    for ind, (c1, c2) in enumerate(zip(s1, s2)):
        if c1 == c2:
            sm += 1
            index_ch.append((ind, c1))
    return index_ch, sm

index_char, sm = paired("house", "mouse")

print(index_char, sm)

Output:

([(1, 'o'), (2, 'u'), (3, 's'), (4, 'e')], 4)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

If you want to preserve the position and character of the matches, you can enumerate the strings, then calculate the intersection of the sets of the resulting tuples. If you don't want to preserve any information about the nature of the matches, I think Padraic's answer is better.

Demo:

>>> s1 = 'hello world'
>>> s2 = 'xelxx worxx'
>>> same = set(enumerate(s1)).intersection(enumerate(s2))
>>> same
set([(7, 'o'), (2, 'l'), (1, 'e'), (8, 'r'), (6, 'w'), (5, ' ')])
>>> len(same)
6
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • I have just tried my code that i showed above on my other pc, and it worked fine then suddenly wouldnt print out 'result'. this is the same for the original computer i was coding it on, the code seems to run fine then for no reason it stops printing out 'result' a few times then will resume back to working correctly – RhysBuddy Apr 06 '16 at 15:47