comparing strings (or sequences) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: comparing strings (or sequences) (/thread-1779.html) |
comparing strings (or sequences) - Skaperen - Jan-25-2017 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. RE: comparing strings (or sequences) - buran - Jan-25-2017 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? RE: comparing strings (or sequences) - Skaperen - Jan-25-2017 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. RE: comparing strings (or sequences) - wavic - Jan-25-2017 Python difflib? I never used it and don't know what it provides RE: comparing strings (or sequences) - buran - Jan-25-2017 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#)? RE: comparing strings (or sequences) - Skaperen - Jan-25-2017 (Jan-25-2017, 09:40 AM)buran Wrote: given that question is "find how many characters are the same": 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 providesit looks like it could be the thing:
|