特定字符串是否与文本fi中的字符串匹配

2024-09-26 19:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含许多单词的文本文件(每行一个单词)。我必须读入每个单词,修改单词,然后检查修改后的单词是否与文件中的任何单词匹配。我在最后一部分遇到了问题(这是我代码中的hasMatch方法)。这听起来很简单,我知道我应该做什么,但无论我尝试什么都行不通。你知道吗

#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()

Tags: andtoinstringifline单词myfile
2条回答

在您的代码中,您不只是分割第一个和最后一个字符,而是分割第一个和最后两个字符。你知道吗

rmFirstLast = str[1:len(str)-2] 

改为:

rmFirstLast = str[1:len(str)-1] 

这里有几个问题

  1. 您必须删除行,否则会得到linefeed/CR字符,使匹配失败
  2. 您必须一次性地读取文件,否则文件迭代器将在第一次读取后耗尽
  3. 速度不好:使用set而不是list加快搜索速度
  4. 切片过于复杂和错误:str[1:-1]做到了(感谢那些评论我答案的人)
  5. 整个代码实在太长太复杂了。我总结了几行。你知道吗

代码:

#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")

我在一个普通英语单词列表上测试了这个程序,得到了以下匹配结果:

so found (was most)
or found (was from)
no found (was long)
on found (was know)
to found (was both)

Edit:另一个版本,它删除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")

相关问题 更多 >

    热门问题