Python Forum

Full Version: comparing strings (or sequences)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i want to compare two strings (maybe this has to be sequences) and find how many characters are the same.  all i find are things like == and >.  but those test if the whole value ofone is high, lower, or equal to the whole value of the other.

no code is posted because i haven't figured out what to do, yet.
would you elaborate more and provide an example? If I understand you right 'war' and 'raw' will yield full "equality", 'goat' and 'boat' will yield 3, etc?
count_of_equal_items('skaperen','skapare') -> 4
count_of_equal_items('war','raw') -> 0
count_of_equal_items('a','a') -> 1
count_of_equal_items([0,1,2,3,4,5,6],[0,1]*9) -> 2
count_of_equal_items('a','b') -> 0
count_of_equal_items('','hello')-> 0
count_of_equal_items('heavy','hello') -> 2

what i am wondering is whether i should implement count_of_equal_items() or not, and if so, is there a way to do it, short of brute force python, that i overlooked in the docs.
Python difflib? I never used it and don't know what it provides
given that question is "find how many characters are the same":

count_of_equal_items('skaperen','skapare') -> 4, why not 5 (skap#r##) or 6 (skap#re##)?
count_of_equal_items('war','raw') -> 0, why not 1 (#a#)?
(Jan-25-2017, 09:40 AM)buran Wrote: [ -> ]given that question is  "find how many characters are the same":

count_of_equal_items('skaperen','skapare') -> 4, why not 5 (skap#r##) or 6 (skap#re##)?
count_of_equal_items('war','raw') -> 0, why not 1 (#a#)?

i should have stated it in a different way:  what is the length of the longest prefix in common

sorry that i worded it poorly.

an implementation would start with an index of 0 incrementing by 1 at the end of the loop, breaking out of the loop when an unequal comparison happens or the end of either argument is reached, returning the index where it was unequal or ended which is also the length of the common prefix.

(Jan-25-2017, 09:32 AM)wavic Wrote: [ -> ]Python difflib? I never used it and don't know what it provides
it looks like it could be the thing:
Output:
[GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import difflib >>> sm = difflib.SequenceMatcher() >>> sm.set_seq1('skaperen') >>> sm.set_seq2('skapare') >>> sm.find_longest_match(0,7,0,7).size 4 >>>