This example describes how Python looks for similar words. Share to everyone for your reference. The specific analysis is as follows:
Problem:
Give you a word a, if you can get another word B by exchanging the order of the letters in the word, then definition B is the brother word for a. Now give you a dictionary, the user input a word, let you according to the dictionary to find out how many brothers words the word.
The Python code is as follows:
From Itertools import Tee,izip to collections import Defaultdict def pairwise (iterable): A, B = tee (iterable) for E Lem in B:break return Izip (A, b) buf_array=[] buf_no={} key_from_id=0 def add_to_buf (word): Global Key_from_id,bu
F_array If Len (word) ==1:pass #TODO for Pos,pair in enumerate (pairwise (word)): If Len (Buf_array) <pos+1: Buf_array.append (Defaultdict (set)) Pos_dict=buf_array[pos] Key=list (pair) Key.sort () key= "". Join (Ke Y) if key not in buf_no:buf_no[key]=key_from_id key_from_id+=1 Key=buf_no[key] Pos_dict[key].add ( WORD) def find_in_buf (word): Global key_from_id,buf_array If Len (word) ==1:pass #TODO exist = [] for pos,p Air in Enumerate (pairwise (word)): If Len (Buf_array) <pos+1:return Pos_dict=buf_array[pos] Key=list ( Pair) Key.sort () key= "". Join (key) if key not in Buf_no:continue Key=buf_no[key] if key not in P
Os_dict:continue Exist.append (Pos_dict[key]) count_dict=defaultdict (int) for i_set in Exist:for i in I_set:count_dict[i ]+=1 result=[] Min_match = Len (Word)-3 for K,v in Count_dict.iteritems (): If V>=min_match:result.appen D (k) return result Add_to_buf ("1234") add_to_buf ("ABCD") add_to_buf ("Cabd") Print find_in_buf ("ACBD")
I hope this article will help you with your Python programming.