遍历每个单词后如何用换行符返回单词中的字母?

时间:2019-02-17 23:00:11

标签: python

我是python的新手,我想只返回单词中的元音,但每个单词的元音都有自己的行。

import sys
import re

line = sys.stdin.readline()
pattern = re.compile("[a-zA-Z0-9]+")


while line:
    for word in pattern.findall(line):
        for letter in word:
            if letter in ('aeiouAEIOU'):
                print(letter.lower(), end="")


    line = sys.stdin.readline()

如果输入是文本文件:

cat
CHO
tree
guess!
bear"

我想返回:

a
o
ee
ue
ea

现在我得到:

aoeeueea 

2 个答案:

答案 0 :(得分:1)

您可以在外循环的末尾打印换行符:

while line:
    for word in pattern.findall(line):
        for letter in word:
            if letter in ('aeiouAEIOU'):
                print(letter.lower(), end="")
        print()
    line = sys.stdin.readline()

答案 1 :(得分:0)

while line:
    for word in pattern.findall(line):
        print(''.join(letter.lower() for letter in word if letter.lower() in "aeiou"))
    line = sys.stdin.readline()