我想要的是删除其中包含两个以上连续元音的单词。所以输入:
s = " There was a boat in the rain near the shore, by some mysterious lake"
输出:
[boat,rain,near,mysterious]
所以这是我的代码。 我只是想知道是否有更好的方法来做到这一点或这是否足够有效。如果你能用python dict或列表做到这一点好吗? :)我是python的新手,所以是的。 :)评论会很好。
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
答案 0 :(得分:5)
或者,您可以始终使用正则表达式和列表推导来获取单词列表:
>>> import re
>>> [x for x in s.split() if re.search(r'[aeiou]{2}', x)]
['boat', 'rain', 'near', 'mysterious']
s.split()
将句子拆分为单词列表。表达式[x for x in s.split()]
依次考虑此列表中的每个单词。
表达式的re.search(r'[aeiou]{2}', x)
部分在每个单词中搜索来自组[aeiou]
的两个连续字母。只有找到两个连续的元音才会将单词放入新列表中。
答案 1 :(得分:4)
使用套装:
使用set.intersection的第一种方法只能找到不相同的连续对,因此oo
不匹配:
s = " There was a boat in the rain near the shore, by some mysterious lake"
vowels = "aeiouAEIOU"
print([x for x in s.split() if any(len(set(x[i:i+2]).intersection(vowels))== 2 for i in range(len(x))) ])
['boat', 'rain', 'near', 'mysterious']
方法2使用set.issubset,因此现在相同的连续对将被视为匹配。
使用set.issubset
使用yield from
python 3语法的函数,这可能更合适,实际上是为了捕获重复的相同元音:
vowels = "aeiouAEIOU"
def get(x, step):
yield from (x[i:i+step] for i in range(len(x[:-1])))
print([x for x in s.split() if any(set(pr).issubset(vowels) for pr in get(x, 2))])
或者再次在单个列表comp:
print([x for x in s.split() if any(set(pr).issubset(vowels) for pr in (x[i:i+2] for i in range(len(x[:-1]))))])
最后让元音成为一组并检查它是否是任何一对字符的set.issuperset:
vowels = {'a', 'u', 'U', 'o', 'e', 'i', 'A', 'I', 'E', 'O'}
def get(x, step):
yield from (x[i:i+step] for i in range(len(x[:-1])))
print([x for x in s.split() if any(vowels.issuperset(pr) for pr in get(x, 2))])
答案 2 :(得分:3)
使用成对迭代:
from itertools import tee
def pairwise(iterable):
a, b = tee(iter(iterable))
next(b)
return zip(a,b)
vowels = 'aeiouAEIOU'
[word for word in s.split() if any(
this in vowels and next in vowels for this,next in pairwise(word))]
答案 3 :(得分:1)
改为使用正则表达式:
import re
s = 'There was a boat in the rain near the shore, by some mysterious lake'
l = [i for i in s.split(' ') if re.search('[aeiou]{2,}', i)]
print ' '.join(l) # back to string
答案 4 :(得分:1)
改用产品:
from itertools import product
vowels = 'aiueo'
comb = list(product(vowels, repeat=2))
s = " There was a boat in the rain near the shore, by some mysterious lake"
def is2consecutive_vowels(word):
for i in range(len(word)-1):
if (word[i], word[i+1]) in comb:
return True
return False
print [word for word in s.split() if is2consecutive_vowels(word)]
# ['boat', 'rain', 'near', 'mysterious']
或者如果您不需要使用任何外部库:
vowels = 'aeiou'
def is2consecutive_vowels2(word):
for i in range(len(word)-1):
if word[i] in vowels and word[i+1] in vowels:
return True
return False
print [word for word in s.split() if is2consecutive_vowels2(word)]
# ['boat', 'rain', 'near', 'mysterious']
这个比正则表达式解决方案更快!
答案 5 :(得分:0)
tbl <- tbl %>% group_by(profession) %>% mutate(rank = ntile(10))