Summary of string search operations in Python and python operations
Basic string location search method
Python searches for strings using variables. find ("content to be searched" [, start position, end position]), start position and end position, indicating the range to be searched. If it is null, it indicates searching for all. After searching, the system returns the position starting from 0. if it finds the position, the system returns-1.
Str = 'a, hello' print str. find ('hello') # search for string hello> 2 # output result in string str
Simple Matching Algorithm
The simple matching algorithm is a one-to-one match between the target string and the template string. If yes, move the lower mark to the right. Otherwise, clear and start matching 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 characters are equal, the lower mark of the target and template is shifted to the right if target [I] = pattern [j]: I, j = I + 1, j + 1 else: # if the characters are not equal, the target subscript is switched to an unequal subscript # Move the template subscript to the initial subscript I = I-j + 1 j = 0 if j = m: return I-j return-1
Print the preceding print and then print it again.
# Else: I = I-j + 1 j = 0 print (target [I], pattern [j], I, j) # print the result B a 1 0b a 2 0 a 3 0a a 4 0
This method is inefficient because the template characters are recyclically repeated when there is no matching. A maximum of m * (n-m + 1) times may occur. M is the length of the template characters, and n-m + 1 is the number of excluded characters.
KMP Algorithm
Kmp is an algorithm for shifting by known matching characters. For example, if abb compared with abc above, AB is known.
Def match (target, pattern): I = j = 0 n, m = len (target), len (pattern) while I <n and j <m: # if the characters are the same, both the target and template's lower mark will be shifted to the right if j =-1 and target [I] = pattern [j]: I, j = I + 1, j + 1 else: # Here we use the next function to determine the number of displacement 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)