我似乎找不到正确的方法来搜索字符串中的最后一个元音,并在最后一个元音之后存储任何唯一的辅音。到目前为止我已经这样设置了。
word = input('Input a word: ')
wordlow = word.lower()
VOWELS = 'aeiou'
last_vowel_index = 0
for i, ch in enumerate(wordlow):
if ch == VOWELS:
last_vowel_index += i
print(wordlow[last_vowel_index + 1:])
最佳答案
我喜欢COLDSPEED's方法,但为了完整起见,我会建议一个基于正则表达式的解决方案:
import re
s = 'sjdhgdfgukgdk'
re.search(r'([^AEIOUaeiou]*)$', s).group(1)
# 'kgdk'
# '[^AEIOUaeiou]' matches a non-vowel (^ being the negation)
# 'X*' matches 0 or more X
# '$' matches the end of the string
# () marks a group, group(1) returns the first such group
请参阅docs on python regular expression syntax 。唯一性部分还需要进一步处理;)
关于python - 查找字符串中的最后一个元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46406958/