需要用 python 编写单词查找器的帮助

发布于 2024-12-05 11:24:20 字数 647 浏览 1 评论 0原文

你好,我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剧终人散尽 2024-12-12 11:24:20

因此,如果这 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!

痕至 2024-12-12 11:24:20

操作方法如下:

  1. 将文件读入 set() 对象,如果使用 readlines(),请不要忘记删除行末尾的 '\n' file 对象的 code> 方法。
  2. 使用 http://docs.python.org/library/itertools 迭代所有排列.html#itertools.permutations 并检查这些排列之一是否在您的集合中。也许您必须将元组映射到字符串,使用 strjoin 方法会很有帮助。

你知道有 9! = 362880 排列?

Here is how to proceed:

  1. read the file into a set() object, do not forget to remove '\n' at the end of the lines if you use the readlines() method of the file object.
  2. iterate over all permutations, using http://docs.python.org/library/itertools.html#itertools.permutations and check if one of these permutations is in your set. Maybe you have to map a tuple to a string, using the join method of str is helpful.

You know that there are 9! = 362880 permutations ?

静水深流 2024-12-12 11:24:20

我首先想到的是场景。

这可能不是理想的解决方案,但应该可以完成工作:

match_letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}
for line in file:
    line = line.strip()
    line_letters = set(line)
    # test whether any letter from match_letters is in line_letters
    if line_letters & match_letters:
        print(line)

OR,如果我误解了并且您正在寻找包含ALL九个字母的单词:

    if line_letters >= match_letters:
        print(line)

OR< /strong>,如果您正在查找包含这九个字母的单词:

    if line_letters <= match_letters:
        print(line)

What pops into my mind first are sets.

This may be not the ideal solution, but should do the job:

match_letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}
for line in file:
    line = line.strip()
    line_letters = set(line)
    # test whether any letter from match_letters is in line_letters
    if line_letters & match_letters:
        print(line)

OR, if I misunderstood and you are looking for words that contain ALL nine letters:

    if line_letters >= match_letters:
        print(line)

OR, if you are looking for words that contain ONLY those nine letters:

    if line_letters <= match_letters:
        print(line)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文