Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拼字算法的优化_Python - Fatal编程技术网

Python 拼字算法的优化

Python 拼字算法的优化,python,Python,我试着写一个算法,通过给它一堆字母,你可以得到所有可以由字母构成的单词,例如,给定的“car”应该返回一个包含[arc,car,a,等等]的列表,然后从中返回最好的拼字。问题在于找到包含所有单词的列表。 我有一个巨大的txt文件字典,用行号分隔,到目前为止我已经尝试过了: def find_optimal(bunch_of_letters: str): words_to_check = [] c1 = Counter(bunch_of_letters.lower()) for word in

我试着写一个算法,通过给它一堆字母,你可以得到所有可以由字母构成的单词,例如,给定的“car”应该返回一个包含[arc,car,a,等等]的列表,然后从中返回最好的拼字。问题在于找到包含所有单词的列表。 我有一个巨大的txt文件字典,用行号分隔,到目前为止我已经尝试过了:

def find_optimal(bunch_of_letters: str):
words_to_check = []
c1 = Counter(bunch_of_letters.lower())

for word in load_words():
    c2 = Counter(word.lower())
    if c2 & c1 == c2:
        words_to_check.append(word)

max_word = max_word_value(words_to_check)
return max_word,calc_word_value(max_word)
max_word_value-返回具有给定列表最大值的单词

计算单词值-返回单词在拼字游戏中的分数

加载单词-返回字典列表

我目前正在使用计数器来执行此操作,但问题是,我当前每次搜索大约需要2.5秒,我不知道如何优化此操作,有什么想法吗?

尝试以下操作:

def find_optimal(bunch_of_letters):

    bunch_of_letters = ''.join(sorted(bunch_of_letters))

    words_to_check = [word for word in load_words() if ''.join(sorted(word)) in bunch_of_letters]

    max_word = max_word_value(words_to_check)

    return max_word, calc_word_value(max_word)
我刚刚使用(或至少尝试使用)了一个
列表
理解。本质上,
words\u to\u check
将(希望!)成为文本文件中所有单词的
列表

顺便说一句,如果你不想用一个巨大的文本文件来写单词,那就去看看吧

输出:

c

a

r

ca

。。。 ar rc ra car cra acr arc rca rac

这将为我们提供一个单词的所有可能组合(即排列)。 如果您想知道生成的单词是否是英语词典中的实际单词,我们可以使用

结论:

如果要生成单词的所有组合。我们使用以下代码:

from itertools import combinations, permutations, product

word = 'word' # or we can use input('Type in a word: ')

solution = permutations(word, 4)

for i in solution:
    print(''.join(i)) # just print(i) if you want a tuple

@实际上,这确实有助于OP,因为它提供了一种不同的方式来实现他们想要的(这是更优化的(这是他们要求的))。@S.Haviv当您拥有OP拥有的所需数据集时,它将编译(列表理解使用未预定义的变量)是的,但当你写“单词”时,你可能是指“单词”吗?load_words()中的word在“”中很常见。join(sorted(words))@Adi219我们必须等待OP的反馈,了解他到底在寻找什么,因为他的代码并不代表复制问题的最小代码。这个例子不太清楚,但是从上面的文字来看,这是我的结论(我的答案)。检查一下,让我知道你的想法!理想情况下,您应该使用字典中的单词创建某种数据结构,以允许快速查找。您知道是哪一种吗?如果字符串长度超过10,则排列函数不是一个选项,它将花费永远的时间。在OP的情况下,这将不起作用。请注意,在OP的示例中,给出了单词car,而“a”是一个可能的建议。如果您运行代码@ninessalt,您将看到它是有效的,而不是将其替换为car。好了,调整好了手术时间code@S.Haviv你说得很对,先生,你有什么建议吗?@ElvirMuslic我不认为OP有实际的运行时间问题,而是一个优化问题(每个查询2.5秒)。我们面临的主要问题是OP使用的是一个自定义类(
Counter
),但是他们没有显示它的定义。我相信,
计数器
可能是优化问题所在,但我无法确定这一点,因为OP尚未公布其定义
import enchant
d = enchant.Dict("en_US")
for word in mylist:
    print(d.check(word), word)
from itertools import combinations, permutations, product

word = 'word' # or we can use input('Type in a word: ')

solution = permutations(word, 4)

for i in solution:
    print(''.join(i)) # just print(i) if you want a tuple