简体   繁体   English

在python中搜索

[英]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. 我已经导入了文件,它可以打开和读取等,但现在我希望能够为任何包含指定字母的单词搜索文件,这些单词必须与人名首相同。

您可以使用itertools (用于排列)和正则表达式 (用于搜索)

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: 我会这样解决这个问题:

  • filter out the words of the length different from the length of the first name, 过滤掉与名字长度不同的长度的单词,
  • iterate over the rest of the words checking whether intersection of first name's letters and word's letters is non-empty ( set might be useful here). 迭代其余的单词,检查名字的字母和单词的字母是否为非空(此处设置可能有用)。

PS Is that your homework? PS那是你的功课吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM