gpt4 book ai didi

python - 查找字符串中的最后一个元音

转载 作者:行者123 更新时间:2023-11-28 21:39:42 26 4
gpt4 key购买 nike

我似乎无法找到正确的方法来搜索最后一个元音的字符串,并在最后一个元音之后存储任何唯一的辅音。到目前为止,我已经这样设置了。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com