我正在尝试获取一个字符串并从超过 4 个字符的单词中删除元音。
有没有更有效的方法来编写这段代码?
(1) 从字符串中创建一个数组。
(2) 遍历一个数组并从超过 4 个字符的字符串中删除元音。
(3) 将数组中的字符串连接到新字符串。
谢谢!
def abbreviate_sentence(sent):
split_string = sent.split()
for words in split_string:
abbrev = [words.replace("a", "").replace("e", "").replace("i", "").replace("o", "").replace("u", "")
if len(words) > 4 else words for words in split_string]
sentence = " ".join(abbrev)
return sentence
print(abbreviate_sentence("follow the yellow brick road")) # => "fllw the yllw brck road"
拉丁的传说
临摹微笑
梦里花落0921
随时随地看视频慕课网APP
相关分类