Python words.txt文件中有多少个元音按顺序排列?
函数Python words.txt文件中有多少个元音按顺序排列?,python,function,python-2.7,return,word,Python,Function,Python 2.7,Return,Word,函数使用将始终返回True。如果我做对了,就只有12个字了 如何使uses返回False def count_vowels(): fin = open ('words.txt') count = 0 vowels = ['aeiou'] for line in fin: word = line.strip() if uses(word, vowels): count = count + 1
使用
将始终返回True。如果我做对了,就只有12个字了
如何使uses
返回False
def count_vowels():
fin = open ('words.txt')
count = 0
vowels = ['aeiou']
for line in fin:
word = line.strip()
if uses(word, vowels):
count = count + 1
print "There are", count ," words with a vowel."
def uses(word, vowels):
for vowels in word:
if vowels in vowels:
return True
return False
def count_words():
fin = open ('words.txt')
vowels = ['aeiou']
for line in fin:
word = line.strip()
if uses(word, vowels):
print word
count_vowels()
count_words()
#This is what returns in the shell when I run my code:
>>>There are 1 words with a vowel.
There are 2 words with a vowel.
There are 3 words with a vowel.
There are 4 words with a vowel.
There are 5 words with a vowel.
....
#and so on...
# However, if I use the following code to replace the middle function in my original code:
def uses(word, vowels):
found = ''
for l in word:
if l not in found:
if l in vowels:
found += l
else:
return False
return found == vowels
# When I run the code the shell returns this:
>>>
>>>
如@void所说,使用
re
模块
import re
s = 'hello world'
words = re.findall(r'\w+', s)
print 'words'''
您只需使用
all
功能即可!让魔法发挥作用:
def uses(word, vowels):
return all(letter in word for letter in vowels)
演示:
这样,您就可以将元音作为“aiueo”
字符串传递,而不是['aiueo']
但是,如果您希望按顺序找到元音,请使用以下命令:
def uses(word, vowels):
found = ''
for l in word:
if l not in found:
if l in vowels:
found += l
else:
return False
return found == vowels
这里,有点令人失望,但我只给你完整的固定代码:
def count_vowels():
with open('words.txt') as fin:
count = 0
vowels = 'aeiou'
for line in fin:
words = line.split()
for word in words:
if uses(word, vowels):
count = count + 1
print "There are", count ," words with a vowel."
def uses(word, vowels):
return all(letter in word for letter in vowels)
def count_words():
with open('words.txt') as fin:
vowels = 'aeiou'
for line in fin:
words = line.split()
for word in words:
if uses(word, vowels):
print word
count_vowels()
count_words()
如果您只查找所有字母都按该顺序排列的单词(但可能中间有其他字母),则需要检查第一个元音,直到找到为止,然后检查第二个元音,然后是第三个元音,直到找到所有字母(或到达单词末尾)。下面是一些代码:
def uses(word, vowels)
vowel_index = 0
for letter in word:
if letter == vowels[vowel_index]:
vowel_index += 1
if vowel_index == len(vowels):
return True
return False
我没有删除发现的元音,而是保留一个整数索引,告诉我当前测试的元音
您需要使用元音的可下标值调用此函数,可以是一个包含一个字符串作为其内容的列表,也可以是一个包含所有元音的字符串。如果我做对了,我将实现一个uses
-函数,如下所示:
def uses(word, vowels):
# remove every letter, that's not a vowel, make the word lower case
reduced_word = filter( lambda character: character in vowels, word.lower() )
# check, whether the vowels are in order
return vowels in reduced_word
vowels = "aeiou"
test_words = [ "atetitotu", "Atetitotu", "Haemodiutory", "FgeAihou" ]
for word in test_words: print word, uses(word, vowels)
输出:
atetitotu True
Atetitotu True
Haemodiutory False
FgeAihou False
但是,如果您想查找文件中的所有这些单词,我建议您使用re
-module:
import re
file_content = open('file.txt').read()
vowels = "aeiou"
# the pattern starts with word boundary and there can be any kind of letters at the beginning
pattern = r"\b\w*"
# than between every vowel there can be any letter, except of a vowel
pattern += (r"[^%s]*?" % vowels ).join(vowels)
# and then, after the last vowel there can be any letter before the word ends
pattern += r"\w*\b"
# here the result: pattern = r"\b\w*a[^aeiou]*?e[^aeiou]*?i[^aeiou]*?o[^aeiou]*?u\w*\b"
# now we look for this pattern all over the file, ignoring case
re.findall(pattern, file_content, flags = re.I)
输出是这样的
['atetitotu', 'Atetitotu']
目前的结果是什么?也,你能给我们一个输入样本吗?'atetitotu'
是否按顺序计算?为什么你把元音都列为一个字符串?这可能是因为你正在从元音中删除字母,但从来没有把它们放回下一个单词中…一个单词中元音的顺序必须是aeiou
?元音aeiou
是否有序?不一定?你什么意思?啊,我明白了。。等一下,OP还没有澄清。我想我有点匆忙:)它应该返回12次:“有”,计数,“带元音的单词。”
['atetitotu', 'Atetitotu']
def uses_only(wrd_trnsv, str_chars):
for chr_trnsv in wrd_trnsv:
if chr_trnsv not in str_chars:
return False
return True
wrd_list = open('C:\\Temp\\Complete_Words_list.txt')
def uses_all_txt(wrd_list, req_str_chrs):
count = 0
for each_wrd in wrd_list :
if uses_only(req_str_chrs,each_wrd):
count = count + 1
return count