Summary of how string lookups are done in Python

Source: Internet
Author: User
Basic string Location Lookup method
The Python lookup string uses variables. FIND ("What to Look for" [, start position, end position]), start and end position, indicate the range to look for, or null to find all. The location is returned after finding it, and the position is calculated from 0, and returns 1 if each is found.

str = ' A,hello ' Print str.find (' hello ') # Find string in string str Hello >> 2     

Simple matching algorithm

The naïve matching algorithm is a one by one match between the target string and the template string. If the match is on, the subscript shifts one bit to the right, otherwise empties and starts the match again.

target = ' abb aba ' pattern = ' aba ' def match (target, pattern):  i = j = 0  n, m = Len (target), Len (pattern) while  I < N and J < m:    # If the word typeface and so on the target and template subscript all move to the right if    target[i] = = Pattern[j]:      i, j = i+1, j+1    else:      # if the character Unequal then the target subscript switches to the unequal subscript      # template subscript moves to the initial subscript      i = i-j + 1      j = 0  If J = = m:    return i-j  return-1

Print out the top

#修改的地方else:  i = i-j + 1  j = 0  print (target[i], pattern[j], I, j) # Print result b a 1 0b a 2 0 a 3 0a a 4 0

The loop will always be equal to the matching value, this method is inefficient, mainly when the mismatch will re-cycle the template characters once. The maximum number of M * (n-m + 1) times may occur. M is the length of the template character, N-m + 1 is the number of times to exclude unequal characters.

KMP algorithm

KMP is an algorithm that is shifted by known matched characters, such as the one in ABB above which is known as the ABC comparison.

def match (target, pattern):  i = j = 0  n, m = Len (target), Len (pattern) while  I < N and J < m:    # if character Equal then the target and the template subscript are shifted to the right    if if j = =-1 and target[i] = = Pattern[j]:      i, j = i+1, j+1    else:      # Here is the next function to determine the number of displacements 
  i = I-j + pattern_next (Pattern[:j])      j = 0  If J = = m:    return i-j return-1def pattern_next  (s): 
  prefix = [s[:i+1] for I in range (len (s)-1)]  suffix = [s[i+1:] for I in range (len (s)-1)]  L = list (set (prefix) & Set (suffix))  return Len (L)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.