Python ways to find similar words _python

Source: Internet
Author: User
Tags iterable

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.

Related Article

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.