如何遍历每一行,并将每一行的结果追加到列表中?

时间:2020-11-10 17:34:45

标签: python

我的文本包含多行,例如:

roses are red
violets are blue
I'm trying to learn python 
please don't be rude

我想计算文本中的每个元音并将每行的元音按顺序存储在列表中:

open (file) as text
vowels = [aeiouy]
line = text.splitlines
point = 0
final_list = []

for line in text:
if line in vowels 
point = point +1
final_list.append(point)

预期:

[5, 6, 6, 7]

会发生什么?

0

1 个答案:

答案 0 :(得分:1)

您没有遍历每一行的每个字符,您可能想要这样做。另外,由于您希望每个元音都是一个单独的字符,因此您的元音列表应类似于vowels = ['a','e','i','o','u']。 也许您想做这样的事情:

for line in text:
    for character in line:
        if character in vowels:
            point = point + 1
    final_list.append(point)
    point = 0
相关问题