每个单词中的python元音计数器

时间:2015-05-14 03:01:31

标签: python python-3.x

如果有元音的话,如何检查一行中每个单词的每个字母

for line in inFile:
        wordlist=line.lower().split()
        count = 0
        for word in wordlist:
            if word[0] in vowels :
                word.count 
                continue
        outFile.write(str(count) + '\n')

2 个答案:

答案 0 :(得分:0)

以此为例,但它可以更短更pythonic:

编辑:

inFile = ('ana john joe', 'eric daniel veronica') 
vowels = 'aeiou'
counts = []
for line in inFile:
    count = 0
    words = line.split()
    for word in words:
        count = 0
        for character in word:
            if character in vowels:
                count+=1
        counts.append(str(count))
print(' '.join(counts))

答案 1 :(得分:0)

即使它们出现多次,也会给出每个单词的计数,这就是你所说的:

for line in inFile:
    wordlist = line.lower().split()
    for word in wordlist:
        count = sum([1 for c in word if c in vowels])
        print("%s = %i" % (word, count))