Python 相似单词,Python单词,给你一个单词a,如果通过
Python 相似单词,Python单词,给你一个单词a,如果通过
给你一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在给你一个字典,用户输入一个单词,让你根据字典找出这个单词有多少个兄弟单词。
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)<pos+1: buf_array.append(defaultdict(set)) pos_dict=buf_array[pos] key=list(pair) key.sort() key="".join(key) 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,pair 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 pos_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.append(k) return resultadd_to_buf("1234")add_to_buf("ABCD")add_to_buf("CABD")print find_in_buf("ACBD")#该片段来自于http://byrx.net
相关内容
- Django 简化view函数的编写,django简化view函数,1.定义包装
- Python 脚本文件批量改名,,#!/usr/bin/e
- Python 清除CVS和SVN下的目录和子目录,cvssvn,#!/usr/bin/e
- Python 修改MP3,python修改mp3,用这个程序修改后的MP3
- Python 抓取新浪读书频道小说,,#!/usr/bin/e
- Python 连接MySQ和MSSQL,pythonmysqmssql,host = ''
- Python ElementTree 解析指定命名空间的xml,pythonelementtree
- Python 发送邮件,python发送邮件,#coding=utf-
- python对文件夹的一些操作,python文件夹操作,python文件夹
- Python 简单的记事本,Python简单记事本,这个程序只实现
评论关闭