Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python从单词中提取元音_Python_Mapreduce - Fatal编程技术网

python从单词中提取元音

python从单词中提取元音,python,mapreduce,Python,Mapreduce,我在读一串单词,并试图检索其中所有的元音,但没有太大的成功。如果我有这个:你好,这是一个测试。我想退回这样的东西: :| eo | 1:| :| i | 1:| :| i | 1:| :| a | 1:| :| i | 1:| :| i | 1:| 这是我的代码的最新版本,它只打印单词,我知道我需要循环每个单词,但不知道如何才能不让它变成一个无休止的循环 import sys import re import string line = sys.stdin.readline() vowels

我在读一串单词,并试图检索其中所有的元音,但没有太大的成功。如果我有这个:你好,这是一个测试。我想退回这样的东西:

:| eo | 1:| :| i | 1:| :| i | 1:| :| a | 1:| :| i | 1:| :| i | 1:| 这是我的代码的最新版本,它只打印单词,我知道我需要循环每个单词,但不知道如何才能不让它变成一个无休止的循环

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()

现在,您只有给定单词中的元音列表。根据需要组合并格式化。

以下是另一个基于您的答案:

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

for idx, word in enumerate(lst):
    new_word = ''
    for letter in word:
        if letter in vowels:
            new_word += letter
            lst[idx] = new_word
print(lst)
#['eo', 'i', 'i', 'a', 'e', 'i', 'i']

我想你想要这样的东西

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} :|")

输出

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