简体   繁体   English

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

[英]need help with programming a word finder in python

hi i have a question about python which im a rookie at: 嗨我有一个关于python的问题,我是一个菜鸟:

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 我有一个文本文件,其中包含按字母顺序排列的单词列表(大约23000),就像一个小字典,每一行都是该文本文件中的一个单词

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 

So if it's an exact match of those 9 letters, we can be a little tricky here. 因此,如果它与这9个字母完全匹配,我们在这里可能会有点棘手。 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. 不是创建所有这些排列并检查每个排列,而是使用python内置sorted函数( doc )将单词排序为字母顺序,并比较结果。

The "trick" here is realizing you're looking for an anagram for those 9 letters. 这里的“诀窍”是意识到你正在寻找这9个字母的字谜。 For Example, 'terse' and 'reset' are anagrams of each other, but if you sort them they both turn into 'eerst'. 例如,'terse'和'reset'是彼此的字谜,但如果你对它们进行排序,它们都会变成'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. 至于程序的其余部分,如果你找一些关于用python读取文本文件的基础教程,我相信你将能够完成剩下的工作。 Good luck! 祝好运!

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. 将文件读入set()对象,如果使用file对象的readlines()方法,请不要忘记删除行末尾的'\\n'
  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. 使用http://docs.python.org/library/itertools.html#itertools.permutations迭代所有排列,并检查其中一个排列是否在您的集合中。 Maybe you have to map a tuple to a string, using the join method of str is helpful. 也许你必须将一个元组映射到一个字符串,使用strjoin方法很有帮助。

You know that there are 9! = 362880 你知道有9! = 362880 9! = 362880 permutations ? 9! = 362880个排列?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM