[英]Condition in Python with letters in a specific order
I am trying to find words in a corpus that have letters (specifically aeiouy) that appear in the words in that order (like facetiously).我正在尝试在语料库中查找具有按该顺序出现在单词中的字母(特别是 aeiouy)的单词(例如滑稽地)。 I have the following code so far, but am struggling on how to get it to have the condition that they need to be in the following order.到目前为止,我有以下代码,但正在努力使其具有它们需要按以下顺序排列的条件。
english = nltk.corpus.words.words()
words = [w for w in english if re.search(r'[aeiouy]',w)]
Is there any easy way to do this?有什么简单的方法可以做到这一点?
Try re.search(r'a[b-zB-Z]*e[b-df-zB-DF-Z]*i[b-df-hj-zB-DF-HJ-Z]*o[b-df-hj-np-zB-DF-HJ-NP-Z]*u[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]*y',w)
to allow for some (eventual) characters ([a-zA-Z]) between you letters.试试re.search(r'a[b-zB-Z]*e[b-df-zB-DF-Z]*i[b-df-hj-zB-DF-HJ-Z]*o[b-df-hj-np-zB-DF-HJ-NP-Z]*u[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]*y',w)
允许字母之间的一些(最终)字符([a-zA-Z])。
Ugly is n't it.丑是不是。
But if you are beforehand sure that all characters are in [a-zA-Z], it can be simplified: re.search(r'a[^a]*e[^ae]*i[^aei]*o[^aeio]*u[^aeiou]*y',w)
但是,如果您事先确定所有字符都在 [a-zA-Z] 中,则可以简化: re.search(r'a[^a]*e[^ae]*i[^aei]*o[^aeio]*u[^aeiou]*y',w)
How about re.search('a[^iouy]*e[^aouy]*i[^aeuy]*o[^aeiy]*u[^aeio]*y', w, re.I)
re.search('a[^iouy]*e[^aouy]*i[^aeuy]*o[^aeiy]*u[^aeio]*y', w, re.I)
This is an optimal way to do it without having to use RegEx:这是无需使用 RegEx 的最佳方法:
def verifyString(s):
current_vowel = 'a'
vowels= 'aeiouy'
for c in s:
if c==current_vowel:
if len(vowels)==1:
return True
vowels = vowels[1:]
current_vowel = vowels[0]
return False
If you want the order to be strict, you can use:如果您希望订单严格,可以使用:
def verifyStringStrict(s):
current_vowel = 'a'
vowels= 'aeiouy'
for c in s:
if c in vowels:
if c==current_vowel:
if len(vowels)==1:
return True
vowels = vowels[1:]
current_vowel = vowels[0]
else:
return False
return False
You will have:你将会有:
verifyString('saseqiqoqusyq') #True
verifyStringStrict('ssasdediqqodudyd') #True
verifyString('zazezizzudozyz') #False
verifyStringStrict('daezizzuzozyz') #False
verifyString('waewwiwuowuwcyd') #True
verifyStringStrict('waeiwuwowuwxyx') #False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.