Python Forum
comparing strings (or sequences)
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comparing strings (or sequences)
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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?
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Python difflib? I never used it and don't know what it provides
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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#)?
Reply
#6
(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 >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 696 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  detect equal sequences in list flash77 17 2,682 Oct-28-2022, 06:38 AM
Last Post: flash77
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  needleman wunsch algorithm for two sequences of different length johnny_sav1992 0 1,674 Jul-27-2020, 05:45 PM
Last Post: johnny_sav1992
  Comparing Values/QC Within Two Strings uttadms31 2 1,876 Jul-07-2020, 03:49 PM
Last Post: uttadms31
  help for escape sequences NewPi 1 1,997 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  copying parts of mutable sequences Skaperen 1 2,187 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  Convert weekly sequences to date and time. SinPy 0 1,421 Nov-23-2019, 05:20 PM
Last Post: SinPy
  Escape sequences display in python Uchikago 1 2,376 Jun-27-2019, 03:25 PM
Last Post: Gribouillis
  Finding multiple strings between the two same strings Slither 1 2,478 Jun-05-2019, 09:02 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020