문제

Given two strings of equal length, how do I return the number of times that the strings have the same character at a given index?

So: count_matches("bob","boa") would give 2 as index 0 holds the same character and so does index 1.

But, count_matches('bob', 'bbo') would only return 1 as the only index where the character is the same in both is index 0. Although there are two 'b's in both, only one of those is at a corresponding index.

도움이 되었습니까?

해결책

I assume you mean the number of indices where the character is the same in both strings.

Therefore, I would do:

>>> sum(a==b for a, b in zip('bob', 'boa'))
2

Wrapping that in a function should be trivial.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top