[英]searching in python
I am trying to search a file to find all words which use any or all of the letters of a persons first name and are the same length as their first name. 我正在尝试搜索文件以查找使用人名的任何或所有字母并且与其名字长度相同的所有单词。 I have imported the file and it can be opened and read etc, but now i want to be able to seach the file for any words which would contain the specified letters, the words have to be same length as the persons first name.
我已经导入了文件,它可以打开和读取等,但现在我希望能够为任何包含指定字母的单词搜索文件,这些单词必须与人名首相同。
def find_anagrams_in_file(filename, searchword):
import re
searchword = searchword.lower()
found_words = []
for line in open(filename, 'rt'):
words = re.split(r'\W', line)
for word in words:
if len(word) == len(searchword):
tmp = word.lower()
try:
for letter in searchword:
idx = tmp.index(letter)
tmp = tmp[:idx] + tmp[idx+1:]
found_words += [word]
except ValueError:
pass
return found_words
Run as so (Python 3): 像这样运行(Python 3):
>>> print(find_anagrams_in_file('apa.txt', 'Urne'))
['Rune', 'NurE', 'ERUN']
I would approach this problem this way: 我会这样解决这个问题:
PS Is that your homework? PS那是你的功课吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.