这是我目前所拥有的,但我被卡住了。我正在使用 nltk 作为单词列表,并试图找到所有带有“沙子”中字母的单词。从这个列表中,我想找到我可以用剩余字母组成的所有单词。
import nltk.corpus.words.words()
pwordlist = []
for w in wordlist:
if 's' in w:
if 'a' in w:
if 'n' in w:
if 'd' in w:
pwordlist.append(w)
在这种情况下,我必须使用所有字母来找到可能的单词。 我认为这将有助于找到剩余字母的可能单词,但我无法弄清楚如何仅删除“沙子”中的字母实例。
puzzle_letters = nltk.FreqDist(x)
[w for w in pwordlist if len(w) = len(pwordlist) and nltk.FreqDist(w) = puzzle_letters]
最佳答案
我会将逻辑分为四个部分:
- 函数
contains(word, letters)
,我们将使用它来检测单词是否包含“sand” - 函数
subtract(word, letters)
,我们将使用它从单词中删除“sand”。 get_anagrams(word)
函数,用于查找单词的所有变位词。- 结合上述所有内容的主要算法,在您删除“sand”后查找与其他词的变位词。
from collections import Counter
words = ??? #todo: somehow get a list of every English word.
def contains(word, letters):
return not Counter(letters) - Counter(word)
def subtract(word, letters):
remaining = Counter(word) - Counter(letters)
return "".join(remaining.elements())
anagrams = {}
for word in words:
base = "".join(sorted(word))
anagrams.setdefault(base, []).append(word)
def get_anagrams(word):
return anagrams.get("".join(sorted(word)), [])
for word in words:
if contains(word, "sand"):
reduced_word = subtract(word, "sand")
matches = get_anagrams(reduced_word)
if matches:
print word, matches
在 Words With Friends 词典上运行上面的代码,我得到了很多结果,包括:
...
cowhands ['chow']
credentials ['reticle', 'tiercel']
cyanids ['icy']
daftness ['efts', 'fest', 'fets']
dahoons ['oho', 'ooh']
daikons ['koi']
daintiness ['seniti']
daintinesses ['sienites']
dalapons ['opal']
dalesman ['alme', 'lame', 'male', 'meal']
...
关于python - 如何删除字符串中 x 字符的 1 个实例并在 Python3 中找到它组成的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29989464/