如何在word文件中找到一个没有字母s且只包含一个元音的7个字母的单词?

How to find a 7 letter word without the letter s and contains only one vowel in a word file?

我被要求编写一个代码来打印来自 "dictionary.txt"(一个 250,000 个单词的文件)的单词,这些单词只包含一个元音,没有字母 "s",并且有 7 个字母长。我知道我必须定义一个函数来打开文件并在其中搜索这些要求。

我不允许使用正则表达式,文件每行一个字。

这是我当前的 python 脚本:

a="a"
e="e"
i="i"
o="o"
u="u"
y="y"



def search():    
    Input=open("dictionary.txt","r") 
    for word in Input:
        word=Input.lower()
        vowel=len(word-a)==6 or len(word-e)==6 or len(word-i)==6 or len(word-o)==6 or len(word-u)==6 or len(word-y)==6
        if len(word)==7 and "s" not in word and vowel==True:
            return word

 print(search())

我不在办公桌旁,所以无法给您一个完整的答案,但我的第一直觉是使用正则表达式 select 您要查找的词。 "re" 图书馆是您要开始的地方。

https://pymotw.com/2/re/

它们需要一点时间来适应,但它们对于筛选字符串非常有用。

如果您对它们完全陌生,有很多像这个 (https://regexone.com/) 这样的交互式教程可以帮助您入门。

假设您的 dictionary.txt 只包含 space 分隔的单词和换行符,这可以通过以下方式完成:

# Open the file and construct a list of single words
with open("dictionary.txt", "r") as infile:
    x = [i.strip() for i in infile.read().split(" ")]

# Function for finding number of vowels in a word
def vowels(word):
    count = 0
    for i in word:
        if i in 'aeoui':
            count += 1
    return count

# Check the length of each word, if it contains s and if the number of vowels is one at most
for i in x:
    if len(i) == 7 and "s" not in i and vowels(i) <= 1:
        print(i)

这可能是使用正则表达式完成任务的最简单的方法。

 with open("dictionary.txt","r") as file: #use r to open in read only mode to not mess with file
    words=[]
    for line in file: #loop through every line to get all words
        words.append(line)
import re

for word in words:
    if len(re.findall('[aeiou]', word)) == 1 and len(word)==7 and "s" not in word: #checks if there is only one vowel and length is 7
        print(word)

编辑: 因为您已经编辑说您不能使用正则表达式,所以您可以这样做。

with open("dictionary.txt","r") as file: 
        words=[]
        for line in file: #loop through every line to get all words
            words.append(line)

for word in words:
    if sum(letter in "aeiou" for letter in word)==1 and "s" not in word and len(word)==7:
        print(word)

假设您将整个字典文件读入一个数组,然后循环遍历该数组(使用 'word' 作为循环变量),将其放在循环之前:

import re

# this to make sure there is no 's' in word and its length is exactly 7 characters
no_s_re = re.compile(r'^[a-rt-z]{7}$', re.IGNORECASE)

# this to count vowels (later)
vowels_re = re.compile(r'[aioue]', re.IGNORECASE)

这是循环体:

if no_s_re.match(word) and len(vowels_re.findall(word)) == 1:
     print word

不需要正则表达式。集是相当快的。

text = open('dictionary.txt').read()

vowels = 'aeiou'
vowelsSet = set(vowels)

for word in text.split():
    word = word.lower()
    if len(word)==7 and not 's' in word and len(set(word)-vowelsSet)==6:
        print (word)

第一行中的 open-read 组合吞噬了单词的集合——假设它不包含除单词中的撇号以外的标点符号并且不超过一行。

通过比较任何给定单词中字符的大小与元音的大小,可以判断元音是否重复。原理是,比如moan字符集大小为4,moon字符集大小为3 .

一个线性正则表达式,用于挑战:

^(?:[b-df-hj-np-rtv-z])*[aeiou](?:[b-df-hj-np-rtv-z])*(?<=\w{7})$
  • (?:[b-df-hj-np-rtv-z])* 不捕获 0 到除 s 之外的许多辅音
  • [aeiou]正好一个元音
  • (?:[b-df-hj-np-rtv-z])* 不捕获 0 到除 s 之外的许多辅音

您现在拥有规则 "exactly one vowel"

  • (?<=\w{7})从这一点回到开头,看看是否匹配:恰好 7 个字母

当然我同意可以进行三个简单的测试以更好地维护。