在字符串中的第一个元音之前计数辅音
问题描述:
我正在创建一个简单的猪拉丁语翻译器。这是我到目前为止有:在字符串中的第一个元音之前计数辅音
while True:
phrase = input('Translate > ').lower().split()
for word in phrase:
if word[0] in 'aeiou': # If the first letter is a vowel
print(word + '-way') # Add suffix 'way'
else:
c = # Number of consonants before the first vowel
print (word[c:] + word[0:c] + '-ay')
我怎样才能让c
等于word
第一个元音尽可能简单地前面的辅音的数量?我不想使用函数。
编辑:是的,我的意思是我不想定义我自己的功能。对于那个很抱歉。
答
from itertools import takewhile
c = len(list(takewhile(lambda x: x not in "aeiou", word)))
takewhile需要predicate
在这里是拉姆达,而谓语是True
所以在这种情况下,一旦我们遇到一个元音将采取元素的方法将停止,返回到该点的辅音列表,我们只需使用len
函数来检查列表中有多少辅音,给我们索引c
。
“宁愿不使用函数”...什么?无论如何,'c =下一步(我为我,c在枚举(单词)如果c在'aeiou')' – roippi 2014-10-02 22:41:01
只是供参考:你正在使用函数。 – sheeptest 2014-10-02 22:43:00
我认为他的意思是“ID宁愿不要写功能”,这种方式很奇怪 – TehTris 2014-10-02 22:44:27