需要用 python 编写单词查找器的帮助
你好,我有一个关于 python 的问题,我是一个菜鸟:
我有一个文本文件,其中包含按字母顺序排列的单词列表(大约 23000 个),就像一本小字典 每一行都是该文本文件中的一个单词,
我必须编写一个程序,要求用户输入九个字母,然后该程序应该重新排列这些字母,并找到文本文件中与这组九个字母匹配的所有单词,
我有点卡住了在这个程序的编码中,我需要一些帮助,
这就是我所做的
Nian = raw_input ("Type in nine letters :")
filename = "dictionary.txt"
fil = open(filename, "r")
lines = fil.read()
tx4 = lines.strip()
a = Nian[0]
b = Nian[1]
c = Nian[2]
d = Nian[3]
e = Nian[4]
f = Nian[5]
g = Nian[6]
h = Nian[7]
i = Nian[8]
for w in lines[0:23005]:
if a or b or c or d or e or f or g or h or i in lines:
print w
hi i have a question about python which im a rookie at:
i have a text file which contains a list of words (around 23000) in alphabetical order, like a small dictionary
each line is a word in that textfile
i have to make a programme that asks the user for nine letters, and then the programme is supposed to rerange these letters and find all words in the textfile which match this set of nine letters
im kind of stuck in the coding of this programme, and i would like some assistance please
this is what i've done
Nian = raw_input ("Type in nine letters :")
filename = "dictionary.txt"
fil = open(filename, "r")
lines = fil.read()
tx4 = lines.strip()
a = Nian[0]
b = Nian[1]
c = Nian[2]
d = Nian[3]
e = Nian[4]
f = Nian[5]
g = Nian[6]
h = Nian[7]
i = Nian[8]
for w in lines[0:23005]:
if a or b or c or d or e or f or g or h or i in lines:
print w
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,如果这 9 个字母完全匹配,我们可能会有点棘手。无需创建所有这些排列并检查每个排列,只需使用 python 内置
sorted
函数(doc) 并比较结果。这里的“技巧”是意识到您正在寻找这 9 个字母的字谜词。例如,
“terse”和“reset”是彼此的字谜词,但如果你对它们进行排序,它们都会变成“eerst”。
即使您不寻找精确匹配,您仍然可以使用此技巧进行优化。
至于程序的其余部分,如果您寻找一些有关使用 python 读取文本文件的基本教程,我相信您将能够完成其余部分。祝你好运!
So if it's an exact match of those 9 letters, we can be a little tricky here. Instead of creating all those permutations and checking each one, merely sort the words into alphabetical order using the python built-in
sorted
function (doc) and compare the result.The "trick" here is realizing you're looking for an anagram for those 9 letters. For Example,
'terse' and 'reset' are anagrams of each other, but if you sort them they both turn into 'eerst'.
Even if you're not looking for exact matches you can still use this trick to make optimizations.
As for the rest of the program, if you look for some basic tutorials on reading a text file with python, I'm sure you'll be able to get through the rest of it. Good luck!
操作方法如下:
'\n'
file
对象的 code> 方法。str
的join
方法会很有帮助。你知道有
9! = 362880 排列?
Here is how to proceed:
'\n'
at the end of the lines if you use thereadlines()
method of thefile
object.join
method ofstr
is helpful.You know that there are
9! = 362880
permutations ?我首先想到的是场景。
这可能不是理想的解决方案,但应该可以完成工作:
OR,如果我误解了并且您正在寻找包含ALL九个字母的单词:
OR< /strong>,如果您正在查找仅包含这九个字母的单词:
What pops into my mind first are sets.
This may be not the ideal solution, but should do the job:
OR, if I misunderstood and you are looking for words that contain ALL nine letters:
OR, if you are looking for words that contain ONLY those nine letters: