This article mainly introduces how to search for similar words in Python, which involves Python's string-based operation skills and has some reference value, for more information about how to search for similar words in Python, see the following example. Share it with you for your reference. The specific analysis is as follows:
Problem:
Let's give you a word a. if another word B can be obtained by exchanging the letter order in the word, B is defined as the brother word of. Now, you can enter a word in a dictionary to find out how many brothers the word has.
The Python code is as follows:
from itertools import tee,izipfrom collections import defaultdictdef pairwise(iterable): a, b = tee(iterable) for elem in b: break return izip(a, b)buf_array=[]buf_no={}key_from_id=0def add_to_buf(word): global key_from_id,buf_array if len(word)==1: pass #TODO for pos,pair in enumerate(pairwise(word)): if len(buf_array)
=min_match: result.append(k) return resultadd_to_buf("1234")add_to_buf("ABCD")add_to_buf("CABD")print find_in_buf("ACBD")
I hope this article will help you with Python programming.