Python Forum
Python: Returning the most frequently occurring substring in a larger string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python: Returning the most frequently occurring substring in a larger string
#1
How can I find the most frequently occurring sub-string in a larger string?

For example if the string 'gactctcagc' is provided, the program will return 'ctc' as it occurs twice in the string (including overlap). 'ct' and 'tc' also occur twice, but the longest string gets priority. I want to be able to account for substrings from 2-6 characters.
Reply
#2
what have you tried so far?
Recommended Tutorials:
Reply
#3
(Feb-09-2018, 06:33 AM)metulburr Wrote: what have you tried so far?
I have found out how to find how many times 1 letter appears in a code:

def longestRepeat(x, y):
    count = 0
    longest = 0
    i = x
    while i in y:
        count+=1
        i = x*(count+1)
    return count
BUT I cannot figure out how to have it search through a given string on it's own for the most common.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding how many times substring is in a string using re module ranbarr 4 2,944 May-21-2021, 06:14 PM
Last Post: nilamo
  Returning data from a pyobjc object to python environment newpythonuser100 3 2,392 Jul-28-2020, 12:08 PM
Last Post: newpythonuser100
  Substring in a string Propoganda 1 2,219 Dec-01-2019, 08:45 AM
Last Post: perfringo
  Find a substring in a dictonary; value substring of key aapurdel 2 7,031 May-31-2019, 06:14 PM
Last Post: ichabod801
  [Discussion] Re. Substring counting Mekire 9 5,249 Jan-22-2018, 01:56 PM
Last Post: wavic
  array replace with 1 when larger than 1 metalray 1 2,579 Nov-01-2017, 07:25 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

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