【发布时间】:2015-02-05 23:38:21
【问题描述】:
我想要的是删除其中包含两个以上连续元音的单词。所以输入:
s = " There was a boat in the rain near the shore, by some mysterious lake"
输出:
[boat,rain,near,mysterious]
这是我的代码。 我只是想知道是否有更好的方法可以做到这一点,或者这是否足够有效。如果你可以用 python dict 或列表来做到这一点,可以吗? :) 我是 python 新手,所以是的。 :) cmets 会很好。
def change(s):
vowel = ["a","e","i","o","u"]
words = []
a = s[:].replace(",","").split()
for i in vowel:
s = s.replace(i, "*").replace(",","")
for i,j in enumerate(s.split()):
if "**" in j:
words.append(a[i])
return words
【问题讨论】:
-
"有两个以上连续元音的单词" 你的意思是有两个或更多个连续元音的单词?否则只有
mysterious计数。 -
出于好奇,当前的解决方案将 ajcr 的胜利(时间)公布了几微秒。
re.search进入 ~21micros,设置交叉切片进入 ~60micros。成对迭代大约 35 微秒。
标签: python performance algorithm