Python 如何打印只有一个元音的单词?

Python 如何打印只有一个元音的单词?,python,python-3.x,Python,Python 3.x,我的代码到目前为止,但由于我太迷路了,它没有做任何接近我希望它做的事情: vowels = 'a','e','i','o','u','y' #Consider 'y' as a vowel input = input("Enter a sentence: ") words = input.split() if vowels == words[0]: print(words) 对于这样的输入: "this is a really weird test" 我希望它只打印: this,

我的代码到目前为止,但由于我太迷路了,它没有做任何接近我希望它做的事情:

vowels = 'a','e','i','o','u','y'
#Consider 'y' as a vowel

input = input("Enter a sentence: ")

words = input.split()
if vowels == words[0]:
    print(words)
对于这样的输入:

"this is a really weird test"
我希望它只打印:

this, is, a, test

因为它们只包含1个元音。

如果您已选中下一个字符是空格,则可以使用一个for循环将子字符串保存到字符串数组中。 对于每个子字符串,检查是否只有一个a、e、i、o、u(元音),如果是,则添加到另一个数组中


然后,从另一个数组中,用空格和逗号连接所有字符串

您可以将所有元音转换为单个元音,并计算该元音:

import string
trans = string.maketrans('aeiouy','aaaaaa')
strs = 'this is a really weird test'
print [word for word in strs.split() if word.translate(trans).count('a') == 1]
试试这个:

vowels = set(('a','e','i','o','u','y'))

def count_vowels(word):
    return sum(letter in vowels for letter in word)

my_string = "this is a really weird test"

def get_words(my_string):
    for word in my_string.split():
        if count_vowels(word) == 1:
            print word
vowels = ('a','e','i','o','u','y')
words = [i for i in input('Enter a sentence ').split() if i != '']
interesting = [word for word in words if sum(1 for char in word if char in vowel) == 1]
结果:

>>> get_words(my_string)
this
is
a
test
还有一个选择:

import re

words = 'This sentence contains a bunch of cool words'

for word in words.split():
    if len(re.findall('[aeiouy]', word)) == 1:
        print word
输出:

This
a
bunch
of
words
不确定是否需要没有元音的单词。如果是这样,只需将
==1
替换为
<2

试试以下方法:

vowels = set(('a','e','i','o','u','y'))

def count_vowels(word):
    return sum(letter in vowels for letter in word)

my_string = "this is a really weird test"

def get_words(my_string):
    for word in my_string.split():
        if count_vowels(word) == 1:
            print word
vowels = ('a','e','i','o','u','y')
words = [i for i in input('Enter a sentence ').split() if i != '']
interesting = [word for word in words if sum(1 for char in word if char in vowel) == 1]

我在这里找到了这么多好的代码,我想展示一下我丑陋的代码:

v = 'aoeuiy'
o = 'oooooo'

sentence = 'i found so much nice code here'

words = sentence.split()

trans = str.maketrans(v,o)

for word in words:
    if not word.translate(trans).count('o') >1:
        print(word)
我觉得你缺少正则表达式令人不安

下面是一个纯正则表达式的解决方案:


这个问题是用python标记的,字符串数组?你需要引用它,它确实存在你的意思是,words=[]是一个列表吗?
“This iis a”
input=input(…)
坏主意-您正在重新绑定内置输入,因此下次尝试使用时,它会失败。我不确定这是否更容易阅读,但可能
sum(单词中的字母为1,元音中的字母为1)
也会起作用。如果
计数('a'),则短路版本会停止,怎么样
进入
2
?@gnibler——在大多数情况下,我发现简单的
计数
比短路版本的性能差,这仅仅是因为与发电机相关的开销(以及
str.count
的效率)。例如,是的,这是开玩笑。你的版本比我的快(至少对于给定字符串)顺便说一句。我想这是因为
str.translate
在进行翻译时比删除要快。@gnibler——也许吧。你还有两个全局查找,每个单词都有
len
,而我没有。对于较小的字符串,这可能是一个性能考虑因素……避免导入
string
的聪明方法我需要并且只需要在子字符串上迭代一次(我的子字符串迭代了两次)。我不确定我是想阅读这个版本还是我的版本,但是…+1
[w for w in words.split(),如果re.match(“[^aeiouy]*[aeiouy][^aeiouy]*$,w)
。regex的速度很慢,我建议编译正则表达式(在Kenosis和gnibbler中)。模式是固定的,正则表达式可能会被多次重用。编译的正则表达式非常快。