列表中字符串之间的字母组合 [英] Letter combinations between a string in a list

查看:97
本文介绍了列表中字符串之间的字母组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将给定字符串与列表的区别进行比较。准确地说,如果该单词的仅一个字母与我不同,我正尝试将给定的单词与我的单词列表进行比较。

I'm trying to compare the difference of a given string to a list. Precisely I'm trying to compare a given word, if only one letter of the word was different, to my list of words.

list = ['fake','bake','sake','rake'] #probably a set

如果给定单词​​是 take ,则结果将返回假烤清酒耙子

If given word was take then the result would return fake bake sake rake

如果单词是,返回的是 bake

If the word was bare then the return is bake

我打算这样做的方法是将给定的单词拆分为并开始一个循环,将这个单词的每个字母与字典列表(a,b,c)互换。在循环的每次迭代中,我计划检查该单词是否在我的单词列表中。

The way I'm planning to do this is to split the given word into and start a loop to interchange every letter of this word with a list of the dictionary (a,b,c's). With every iteration of my loop, I plan to check if this word is in my list of words.

我只计算了4个字母的单词,为了检查每个字母组合以匹配我的单词列表,我必须做大约26 ^ 4个循环。

I calculated for just a 4 letter word, I would have to do about 26^4 loops in order to check every letter combination to match my list of words.

有人可以告诉我一种检查单词组合的有效方法吗?

Can someone show me an efficient way to check combinations of a word?

推荐答案

水母库可以计算单词之间的整个距离。

The jellyfish library can calculate a whole host of distances between words. It will probably be better to use this wheel rather than inventing one of your own.

在示例页面中:

>>> import jellyfish
>>> jellyfish.levenshtein_distance('jellyfish', 'smellyfish')
2
>>> jellyfish.jaro_distance('jellyfish', 'smellyfish')
0.89629629629629637
>>> jellyfish.damerau_levenshtein_distance('jellyfish', 'jellyfihs')
1

因此应用于您的问题:

import jellyfish
target = 'take'
list = ['teak','fake','bake','sake','rake','sale']
outlist = [x for x in list if jellyfish.levenshtein_distance(x,target) == 1]

print outlist
['fake', 'bake', 'sake', 'rake']

这篇关于列表中字符串之间的字母组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆