删除其中有 2 个连续元音的单词
Delete words which have 2 consecutive vowels in it
我想要的是删除其中包含两个以上连续元音的单词。所以输入:
s = " There was a boat in the rain near the shore, by some mysterious lake"
输出:
[boat,rain,near,mysterious]
这是我的代码。
我只是想知道是否有更好的方法来做到这一点,或者如果您可以使用 python 字典或列表来做到这一点,这种方法是否有效 enough.And 是否可以? :) 我是 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
或者,您始终可以使用正则表达式和列表理解来获取单词列表:
>>> 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]
中的两个连续字母。只有找到两个连续的元音才会将单词放入新列表中。
改用正则表达式:
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
使用集合:
使用 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))])
或者在单个列表中再次组合:
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))])
使用成对迭代:
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))]
改用产品:
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']
这个比正则表达式解决方案还要快!
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"))
我想要的是删除其中包含两个以上连续元音的单词。所以输入:
s = " There was a boat in the rain near the shore, by some mysterious lake"
输出:
[boat,rain,near,mysterious]
这是我的代码。 我只是想知道是否有更好的方法来做到这一点,或者如果您可以使用 python 字典或列表来做到这一点,这种方法是否有效 enough.And 是否可以? :) 我是 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
或者,您始终可以使用正则表达式和列表理解来获取单词列表:
>>> 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]
中的两个连续字母。只有找到两个连续的元音才会将单词放入新列表中。
改用正则表达式:
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
使用集合:
使用 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))])
或者在单个列表中再次组合:
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))])
使用成对迭代:
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))]
改用产品:
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']
这个比正则表达式解决方案还要快!
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"))