Python I';我在文件中查找字符串,似乎不起作用
我的函数首先计算给定单词的所有可能的字谜。然后,对于这些字谜中的每一个,它会检查它们是否是有效的单词,但会检查它们是否等于wordlist.txt文件中的任何单词。这个文件是一个巨大的文件,有一行一行的单词。所以我决定读每一行,检查每一个字谜是否都有。然而,结果却是一片空白。这是我的密码:Python I';我在文件中查找字符串,似乎不起作用,python,list,file,Python,List,File,我的函数首先计算给定单词的所有可能的字谜。然后,对于这些字谜中的每一个,它会检查它们是否是有效的单词,但会检查它们是否等于wordlist.txt文件中的任何单词。这个文件是一个巨大的文件,有一行一行的单词。所以我决定读每一行,检查每一个字谜是否都有。然而,结果却是一片空白。这是我的密码: def perm1(lst): if len(lst) == 0: return [] elif len(lst) == 1: return [lst]
def perm1(lst):
if len(lst) == 0:
return []
elif len(lst) == 1:
return [lst]
else:
l = []
for i in range(len(lst)):
x = lst[i]
xs = lst[:i] + lst[i+1:]
for p in perm1(xs):
l.append([x] + p)
return l
def jumbo_solve(string):
'''jumbo_solve(string) -> list
returns list of valid words that are anagrams of string'''
passer = list(string)
allAnagrams = []
validWords = []
for x in perm1(passer):
allAnagrams.append((''.join(x)))
for x in allAnagrams:
if x in open("C:\\Users\\Chris\\Python\\wordlist.txt"):
validWords.append(x)
return(validWords)
print(jumbo_solve("rarom"))
如果已经放入许多打印语句进行调试,并且传入的列表“allAnagrams”功能齐全。例如,输入“rarom,一个有效的字谜是单词“armor”,它包含在wordlist.txt文件中。但是,当我运行它时,由于某种原因,它不会检测到是否存在。再次感谢,我对Python还是有点陌生,因此非常感谢您提供的所有帮助,谢谢!您错过了以下一个很小但很重要的方面:
word in open("C:\\Users\\Chris\\Python\\wordlist.txt")
这将逐行搜索文件,就好像使用了open(…).readlines()
,并尝试将整行与'\n'
匹配,最后。实际上,任何需要迭代open(…)
的操作都类似于readlines()
你需要
x+'\n'打开(“C:\\Users\\Chris\\Python\\wordlist.txt”)
如果该文件是一个单独行上的单词列表,则可以通过此操作修复您所拥有的内容,但在每次函数调用中执行此操作效率低下。最好执行一次:
wordlist = open("C:\\Users\\Chris\\Python\\wordlist.txt").read().split('\n')
如果文件是'\n'
分隔单词列表,这将创建单词列表。注意,您可以使用
`readlines()`
而不是read().split('\n')
,但这将像您所做的那样在每个单词上保留\n
,并且您需要将其包括在搜索中,如我上面所示。现在您可以将列表用作全局变量或函数参数
if x in wordlist: stuff
注:Graphier在评论中提出了一项重要建议。一组:
wordlist = set(open("C:\\Users\\Chris\\Python\\wordlist.txt").read().split('\n'))
比列表更适合单词查找,因为它是O(单词长度)。您忽略了以下一个很小但很重要的方面:
word in open("C:\\Users\\Chris\\Python\\wordlist.txt")
这将逐行搜索文件,就好像使用了open(…).readlines()
,并尝试将整行与'\n'
匹配,最后。实际上,任何需要迭代open(…)
的操作都类似于readlines()
你需要
x+'\n'打开(“C:\\Users\\Chris\\Python\\wordlist.txt”)
如果该文件是一个单独行上的单词列表,则可以通过此操作修复您所拥有的内容,但在每次函数调用中执行此操作效率低下。最好执行一次:
wordlist = open("C:\\Users\\Chris\\Python\\wordlist.txt").read().split('\n')
如果文件是'\n'
分隔单词列表,这将创建单词列表。注意,您可以使用
`readlines()`
而不是read().split('\n')
,但这将像您所做的那样在每个单词上保留\n
,并且您需要将其包括在搜索中,如我上面所示。现在您可以将列表用作全局变量或函数参数
if x in wordlist: stuff
注:Graphier在评论中提出了一项重要建议。一组:
wordlist = set(open("C:\\Users\\Chris\\Python\\wordlist.txt").read().split('\n'))
比列表更适合单词查找,因为它是O(字长)。您以错误的方式使用了以下代码:
if x in open("C:\\Users\\Chris\\Python\\wordlist.txt"):
相反,请尝试以下代码,它将解决您的问题:
with open("words.txt", "r") as file:
lines = file.read().splitlines()
for line in lines:
# do something here
您以错误的方式使用了以下代码:
if x in open("C:\\Users\\Chris\\Python\\wordlist.txt"):
相反,请尝试以下代码,它将解决您的问题:
with open("words.txt", "r") as file:
lines = file.read().splitlines()
for line in lines:
# do something here
因此,将所有建议放在一起,您的代码可以简单到:
from itertools import permutations
def get_valid_words(file_name):
with open(file_name) as f:
return set(line.strip() for line in f)
def jumbo_solve(s, valid_words=None):
"""jumbo_solve(s: str) -> list
returns list of valid words that are anagrams of `s`"""
if valid_words is None:
valid_words = get_valid_words("C:\\Users\\Chris\\Python\\wordlist.txt")
return [word for word in permutations(s) if word in valid_words]
if __name__ == "__main__":
print(jumbo_solve("rarom"))
因此,将所有建议放在一起,您的代码可以简单到:
from itertools import permutations
def get_valid_words(file_name):
with open(file_name) as f:
return set(line.strip() for line in f)
def jumbo_solve(s, valid_words=None):
"""jumbo_solve(s: str) -> list
returns list of valid words that are anagrams of `s`"""
if valid_words is None:
valid_words = get_valid_words("C:\\Users\\Chris\\Python\\wordlist.txt")
return [word for word in permutations(s) if word in valid_words]
if __name__ == "__main__":
print(jumbo_solve("rarom"))
哦,谢谢你的澄清。我不知道你可以这样使用文件对象。哦,谢谢你的澄清。我不知道你可以这样使用文件对象。注意,你的
perm1
函数可以替换为调用itertools.permutations()
。您的解决方案具有非常高的复杂性,根据各种约束条件,可以通过多种方式降低复杂性bro…注意,perm1
函数可以替换为调用itertools.permutations()
。您的解决方案具有非常高的复杂性,根据各种约束条件,可以通过多种方式降低复杂性bro.。由于您只读取一次文件,因此最好将其存储在集中,以获得O(1)
中的时间复杂度。非常感谢kabanus!您的回答非常有用,我完全理解我的程序为什么会在这里出错。感谢您只读取了一次文件,最好将其存储在集合中以获得O(1)
中的时间复杂度。非常感谢!您的回答非常有用,我完全理解为什么我的程序在这里出错。谢谢