Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

mapreduce - python extract vowels from words

I am reading in a string of words and trying to retrieve all the vowels in them but not having much success. so if I have this: Hello this is a test this is. I want to return something like this:

:|eo| 1 :|

:|i | 1 :|

:|i | 1 :|

:|a | 1 :|

:|i | 1 :|

:|i | 1 :|

Here is the latest version of my code which just prints the word, I know I need to loop through each word but can't figure out how without it turning into an endless loop.

import sys
import re
import string

line = sys.stdin.readline()
vowels = ['a', 'e', 'i', 'o', 'u']
line = line.lower()
line = line.replace(',', ' ').replace(';',' ').replace('”','').replace('?', .').replace('!','.')
word = line.split()

print(word)
line = sys.stdin.readline()
question from:https://stackoverflow.com/questions/66057534/python-extract-vowels-from-words

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you want something like this

string = "hello this is a test this is"
vowels = ["a", "e", "i", "o", "u"]

for word in string.split(" "):
    new_word = ""
    for char in word:
        new_word = new_word + char if char in vowels else new_word
    print(f"|: {new_word} :|")

output

|: eo :|
|: i :|
|: i :|
|: a :|
|: e :|
|: i :|
|: i :|

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...