从字母列表中找出所有可以使用的英语单词,每个字母的使用次数不超过列表中出现的次数

2024-04-24 16:48:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试将随机字母集输入到函数中,以便它返回文本文件中所有可能的单词,这些单词可以由这些随机字母组成,长度在4到9个字符之间。此时,代码返回仅由集合中的字母组成的单词,但在某些情况下,它会多次使用元素来生成单词。我希望它只输出使用每个字母一次的单词。例如,“animal”将返回,但它已经使用了两次字母“a”来组成这个词

letterList = ["a", "n", "i", "b", "s", "l", "s", "y", "m"] 

with open('american-english') as f:
    for w in f:
        w = w.strip()
        cond = all(i in letterList for i in w) and letterList[4] in w
        if 9 > len(w) >= 4 and cond:
            print(w)

Tags: and函数代码in元素forwith字母
1条回答
网友
1楼 · 发布于 2024-04-24 16:48:54

一个简单的选择可能是使用您现有的方法来比较每个字母的计数

您还可以尝试使用itertools.permutations从字母中生成所有可能的“单词”,并检查每个单词是否都在字典中。我怀疑这将是缓慢的,因为数量的排列将是巨大的,其中大部分不会是文字

寻找字谜的一种常见方法是按字母顺序对两个单词的字母进行排序,然后进行相等比较:

sorted(word1)==sorted(word2)

如果这是真的,那么word1和word2就是anagrams。您可以使用此方法来减少比较的数量,因为使用此技术,您只需要排序后唯一的排列

我已经编写了一个脚本来显示所有三个工作,并允许您对它们进行基准测试。我的测试表明,当字母列表变长时,未定义的itertools方法的伸缩性非常差。计数方法一般,但改进的itertools方法通常最快。当然,这些都可以进一步优化。和他们一起去吧

import time
import itertools

letterList = list('catd')

#letter counting method
tic=time.time()
with open(r'D:/words_alpha.txt','r') as f:
    for word in f:
        if all([word.strip().count(letter) <= letterList.count(letter) for letter in word]):
            print(word.strip())
toc=time.time()
print(toc-tic)

#permutations with no refinement
tic=time.time()
with open(r'D:/words_alpha.txt','r') as f:
    for word in f:
        for n in range(1,len(letterList)+1):
            for pseudoword in itertools.permutations(letterList,n):
                if word.strip() == "".join(pseudoword):
                    print(word.strip())
toc=time.time()
print(toc-tic)

#permutations with anagram refinement
tic=time.time()
pwords=[]
for n in range(1, len(letterList) + 1):
    for pseudoword in itertools.permutations(letterList, n):
        if sorted(pseudoword) == list(pseudoword):
            pwords.append("".join(pseudoword))
print (pwords)
with open(r'D:/words_alpha.txt', 'r') as f:
    for word in f:
        if "".join(sorted(word.strip())) in pwords:
            print(word.strip())
toc=time.time()
print(toc-tic)

相关问题 更多 >