查找文件中给定单词的字谜 [英] Find anagrams of a given word in a file

查看:70
本文介绍了查找文件中给定单词的字谜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,对于班级,我们有这个问题,我们需要能够输入一个单词,并且将从给定的文本文件(wordlist.txt)中使用在文件中找到的该单词的任何字谜来列出.

Alright so for class we have this problem where we need to be able to input a word and from a given text file (wordlist.txt) a list will be made using any anagrams of that word found in the file.

到目前为止,我的代码如下:

My code so far looks like this:

def find_anagrams1(string):
"""Takes a string and returns a list of anagrams for that string from the wordlist.txt file.

string -> list"""
anagrams = []

file = open("wordlist.txt")
next = file.readline()
while next != "":
    isit = is_anagram(string, next)
    if isit is True:
        anagrams.append(next)
    next = file.readline()
file.close()

return anagrams

每次我尝试运行该程序时,尽管我知道存在字谜,但它只会返回一个空列表.有什么问题的想法吗?

Every time I try to run the program it just returns an empty list, despite the fact that I know there are anagrams present. Any ideas on what's wrong?

P.S. is_anagram函数如下所示:

P.S. The is_anagram function looks like this:

def is_anagram(string1, string2):
"""Takes two strings and returns True if the strings are anagrams of each other.

list,list -> string"""
a = sorted(string1)
b = sorted(string2)
if a == b:
    return True
else:
    return False

我正在使用Python 3.4

I am using Python 3.4

推荐答案

问题是您正在使用readline函数.从文档中:

The problem is that you are using the readline function. From the documentation:

file.readline = readline(...)
readline([size]) -> next line from the file, as a string.

Retain newline.  A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.

此处的关键信息是保留换行符".这意味着,如果您有一个包含单词列表的文件(每行一个),则每个单词将以终端换行符返回.因此,当您致电:

The key information here is "Retain newline". That means that if you have a file containing a list of words, one per line, each word is going to be returned with a terminal newline. So when you call:

next = file.readline()

您没有得到example,您得到了example\n,因此它永远不会与您输入的字符串匹配.

You're not getting example, you're getting example\n, so this will never match your input string.

一个简单的解决方案是在从文件读取的行上调用strip()方法:

A simple solution is to call the strip() method on the lines read from the file:

next = file.readline().strip()
while next != "":
    isit = is_anagram(string, next)
    if isit is True:
        anagrams.append(next)
    next = file.readline().strip()
file.close()

但是,此代码存在一些问题.首先,file是一个可怕的变量名,因为它会掩盖python file模块.

However, there are several problems with this code. To start with, file is a terrible name for a variable, because this will mask the python file module.

最好不要利用反复调用readline()的优势,因为打开文件是一个迭代器,它会产生文件的行:

Rather than repeatedly calling readline(), you're better off taking advantage of the fact that an open file is an iterator which yields the lines of the file:

words = open('wordlist.txt')
for word in words:
    word = word.strip()
    isit = is_anagram(string, word)
    if isit:
      anagrams.append(word)
words.close()

在此还请注意,由于is_anagram返回True或False,因此您 无需将结果与TrueFalse(例如if isit is True)进行比较.您可以简单地单独使用返回值.

Note also here that since is_anagram returns True or False, you don't need to compare the result to True or False (e.g., if isit is True). You can simply use the return value on its own.

这篇关于查找文件中给定单词的字谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆