Python Forum
comparing strings (or sequences)
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comparing strings (or sequences)
#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


Messages In This Thread
comparing strings (or sequences) - by Skaperen - Jan-25-2017, 08:35 AM
RE: comparing strings (or sequences) - by buran - Jan-25-2017, 08:45 AM
RE: comparing strings (or sequences) - by Skaperen - Jan-25-2017, 09:32 AM
RE: comparing strings (or sequences) - by wavic - Jan-25-2017, 09:32 AM
RE: comparing strings (or sequences) - by buran - Jan-25-2017, 09:40 AM
RE: comparing strings (or sequences) - by Skaperen - Jan-25-2017, 10:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 778 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  detect equal sequences in list flash77 17 2,851 Oct-28-2022, 06:38 AM
Last Post: flash77
  Splitting strings in list of strings jesse68 3 1,782 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  needleman wunsch algorithm for two sequences of different length johnny_sav1992 0 1,709 Jul-27-2020, 05:45 PM
Last Post: johnny_sav1992
  Comparing Values/QC Within Two Strings uttadms31 2 1,918 Jul-07-2020, 03:49 PM
Last Post: uttadms31
  help for escape sequences NewPi 1 2,043 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  copying parts of mutable sequences Skaperen 1 2,239 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  Convert weekly sequences to date and time. SinPy 0 1,456 Nov-23-2019, 05:20 PM
Last Post: SinPy
  Escape sequences display in python Uchikago 1 2,443 Jun-27-2019, 03:25 PM
Last Post: Gribouillis
  Finding multiple strings between the two same strings Slither 1 2,532 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