Author: Notus(hehe_xiao@qq.com)
Create: 2019-02-25
Update: 2019-02-25
找出文件中元音为'aeiou'的单词
环境
Python version: 3.7.1
代码如下(a.py)
'''
找出文件中的单词,该单词满足规则, 其中的元音按 aeiou 排序
@Author: Notus(hehe_xiao@qq.com)
@Create: 2019-02-25
@Update: 2019-02-25
@Version: 0.1
'''
def cleanword(word):
""" 返回去掉空白的小写字母"""
return word.strip().lower()
def getVowelsInWord(word):
"""返回单词中的元音"""
vowelsInWord = ""
for char in word:
if char in 'aeiou':
vowelsInWord += char
return vowelsInWord
dataFile = open("dictionary.txt", 'r')
print("找到了词典中满足词, 如下:
")
for line in dataFile:
for word in line.split(' '):
word = cleanword(word)
if len(word) < 6:
continue
vowelStr = getVowelsInWord(word)
if vowelStr == 'aeiou':
print(word)
运行与演示
dictionary.txt 文件与 a.py 位于同一目录, 其内容如下
afjwelkfqj wlek jwekq kqke
adfi wlejq wqlfk word kkk
facetious kkfwo book close
were am is are how are you
taheiosu
运行程序
C:UsersNotusDesktop>python a.py
找到了词典中满足词, 如下:
facetious
taheiosu