Summary of string search operations in Python-Python tutorial

Source: Internet
Author: User
Here we will summarize the methods for string search in Python. in addition to the basic find () method, we will also explain how to use the simple matching algorithm and KMP algorithm: 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)

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.