简体   繁体   English

删除包含 2 个连续元音的单词

[英]Delete words which have 2 consecutive vowels in it

What i want is remove the words which have more than two consecutive vowels in it.我想要的是删除其中包含两个以上连续元音的单词。 So input:所以输入:

s = " There was a boat in the rain near the shore, by some mysterious lake"

Output:输出:

[boat,rain,near,mysterious] 

So here is my code.所以这是我的代码。 I was just wondering if there is any better way to do this or is this efficient enough.And if you can do this with python dict or lists are ok?我只是想知道是否有更好的方法来做到这一点,或者这是否足够有效。如果你可以用 python dict 或列表来做到这一点? :) I'm new to python so yeah. :) 我是 python 新手,所以是的。 :) comments would be nice. :) 评论会很好。

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

Alternatively, you could always use regular expressions and list comprehension to get the list of words:或者,您始终可以使用正则表达式和列表理解来获取单词列表:

>>> import re
>>> [x for x in s.split() if re.search(r'[aeiou]{2}', x)]
['boat', 'rain', 'near', 'mysterious']

s.split() splits the sentence into a list of words. s.split()将句子拆分为单词列表。 The expression [x for x in s.split()] considers each word in this list in turn.表达式[x for x in s.split()]依次考虑此列表中的每个单词。

The re.search(r'[aeiou]{2}', x) part of the expression searches each word for two consecutive letters from the group [aeiou] .表达式的re.search(r'[aeiou]{2}', x)部分在每个单词中搜索[aeiou]组中的两个连续字母。 Only if two consecutive vowels are found is the word put in the new list.仅当找到两个连续的元音时,才会将该词放入新列表中。

using sets:使用集合:

First method using set.intersection will only find non identical consecutive pairs so oo would not be a match:使用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']

Method 2 uses set.issubset so now identical consecutive pairs will be considered a match.方法 2 使用set.issubset所以现在相同的连续对将被视为匹配。

using set.issubset with a function using the yield from python 3 syntax which might be more appropriate and indeed to catch repeated identical vowels :set.issubset与使用yield from python 3 语法的yield from的函数一起使用,这可能更合适,并且确实可以捕获重复的相同元音:

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))])

Or again in a single list 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]))))])

Finally make vowels a set and check if it is a set.issuperset of any pair of chars:最后使元音成为一个集合并检查它是否是任何一对字符的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))])

Using pairwise iteration:使用成对迭代:

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))]

Use regular expressions instead:改用正则表达式:

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

Using product instead:改用产品:

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']

or if you don't need to use any external library:或者如果您不需要使用任何外部库:

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']

This one is even quicker than regex solution!这个比正则表达式解决方案还要快!

a=[]
def count(s):
    c=0
    t=s.split()
    for i in t:
        for j in range(len(i)-1):
            w=i[j]
            u=i[j+1]
            if u in "aeiou" and w in "aeiou":
                c+=1
        if(c>=1):
            a.append(i)
        c=0
    return(a)
print(count("There was a boat in the rain near the shore, by some mysterious lake"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM