[英]Does particular string match strings in text file
I have a text file containing many words (single word on each line). 我有一个包含许多单词的文本文件(每行一个单词)。 I have to read in each word, modify the words, and then check if the modified word matches any of the words in the file. 我必须阅读每个单词,修改单词,然后检查修改后的单词是否与文件中的任何单词匹配。 I am having trouble with the last part (it is the hasMatch method in my code). 我在最后一部分上遇到了麻烦(这是我代码中的hasMatch方法)。 It sounds simple enough and I know what I should do, but whatever I try does not work. 听起来很简单,我知道该怎么做,但是无论我尝试什么都行不通。
#read in textfile
myFile = open('good_words.txt')
#function to remove first and last character in string, and reverse string
def modifyString(str):
rmFirstLast = str[1:len(str)-2] #slicing first and last char
reverseStr = rmFirstLast[::-1] #reverse string
return reverseStr
#go through list of words to determine if any string match modified string
def hasMatch(modifiedStr):
for line in myFile:
if line == modifiedStr:
print(modifiedStr + " found")
else:
print(modifiedStr + "not found")
for line in myFile:
word = str(line) #save string in line to a variable
#only modify strings that are greater than length 3
if len(word) >= 4:
#global modifiedStr #make variable global
modifiedStr = modifyString(word) #do string modification
hasMatch(modifiedStr)
myFile.close()
Several problems here 这里有几个问题
set
instead of a list
速度很差:使用set
而不是list
来加快搜索速度 str[1:-1]
does it (thanks to those who commented my answer) 切片过于复杂和错误: str[1:-1]
进行切片(感谢评论了我的答案的人) code: 码:
#read in textfile
myFile = open('good_words.txt')
# make a set (faster search), remove linefeeds
lines = set(x.strip() for x in myFile)
myFile.close()
# iterate on the lines
for word in lines:
#only consider strings that are greater than length 3
if len(word) >= 4:
modifiedStr = word[1:-1][::-1] #do string modification
if modifiedStr in lines:
print(modifiedStr + " found (was "+word+")")
else:
print(modifiedStr + " not found")
I tested the program on a list of common english words and I got those matches: 我在常用英语单词列表上测试了该程序,并找到了匹配项:
so found (was most)
or found (was from)
no found (was long)
on found (was know)
to found (was both)
Edit: another version which drops the set
and uses bisect
on the sorted list to avoid hashing/hash collisions. 编辑:删除版本set
并在已排序列表上使用bisect
以避免哈希/哈希冲突的另一个版本。
import os,bisect
#read in textfile
myFile = open("good_words.txt"))
lines = sorted(x.strip() for x in myFile) # make a sorted list, remove linefeeds
myFile.close()
result=[]
for word in lines:
#only modify strings that are greater than length 3
if len(word) >= 4:
modifiedStr = word[1:-1][::-1] #do string modification
# search where to insert the modified word
i=bisect.bisect_left(lines,modifiedStr)
# if can be inserted and word is actually at this position: found
if i<len(lines) and lines[i]==modifiedStr:
print(modifiedStr + " found (was "+word+")")
else:
print(modifiedStr + " not found")
In your code, you're not slicing just the first and last character but the first and last two characters. 在您的代码中,您不仅要切片第一个和最后一个字符,而且要切片第一个和最后两个字符。
rmFirstLast = str[1:len(str)-2]
Change that to: 更改为:
rmFirstLast = str[1:len(str)-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.