【问题标题】:Condition in Python with letters in a specific orderPython中的条件,具有特定顺序的字母
【发布时间】:2021-11-24 18:22:30
【问题描述】:

我正在尝试在语料库中查找具有按该顺序出现在单词中的字母(特别是 aeiouy)的单词(例如滑稽地)。到目前为止,我有以下代码,但正在努力使其具有它们需要按以下顺序排列的条件。

english = nltk.corpus.words.words()
words = [w for w in english if re.search(r'[aeiouy]',w)]

有什么简单的方法吗?

【问题讨论】:

  • 它让我更接近。它仍然混合字母,aeiouy 仍然不是那个特定的顺序,但它更接近。
  • 这会被视为匹配词吗?轻率地(注意 c 和 e 之间的额外 i)。你能提供一个目前混合字母的例子吗?

标签: python python-re


【解决方案1】:

手动构建正则表达式模式

  • 尝试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])。

    丑是不是。

  • 但如果您事先确定所有字符都在 [a-zA-Z] 中,则可以简化: re.search(r'a[^a]*e[^ae]*i[^aei]*o[^aeio]*u[^aeiou]*y',w)

构建模式的代码

  • 以下代码构建正则表达式模式

    >>>import itertools as it
    >>>seq = "aeiouy"
    >>>pattern = ''.join(it.chain(*[(c, '[^' + seq[0:i+1] + ']*') for i,c in enumerate(seq[:-1])], seq[-1]))
    >>>print(pattern)
    a[^a]*e[^ae]*i[^aei]*o[^aeio]*u[^aeiou]*y
    
  • 总结一下

    # other imports
    import itertools as it
    import regex as re
    
    # words list 
    #english = nltk.corpus.words.words()
    english  = ["facietiously", "aeiouy", "aeioauy"] # mockup, note the second 'a' in the last string
    
    # the sequence
    seq = "aeiouy"
    " compiling the pattern for efficiency 
    seq_re = re.compile(''.join(it.chain(*[(c, '[^' + seq[0:i+1] + ']*') for i,c in enumerate(seq[:-1])], seq[-1])))
    
    words = [w for w in english if seq_re.search(w)]
    
    print(words)
    

    输出:注意单词列表最后一个字符串的过滤:

    ['facietiously', 'aeiouy']
    

【讨论】:

  • @Eddie14,已更新,但一定有更好的方法。
  • 是的,更新版本有效。那应该是完美的。谢谢!
【解决方案2】:

re.search('a[^iouy]*e[^aouy]*i[^aeuy]*o[^aeiy]*u[^aeio]*y', w, re.I)怎么样

【讨论】:

    【解决方案3】:

    这是无需使用 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
    

    如果您希望顺序严格,可以使用:

    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
    

    你将拥有:

    verifyString('saseqiqoqusyq') #True
    verifyStringStrict('ssasdediqqodudyd') #True
    verifyString('zazezizzudozyz') #False
    verifyStringStrict('daezizzuzozyz') #False
    verifyString('waewwiwuowuwcyd') #True
    verifyStringStrict('waeiwuwowuwxyx') #False
    

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      相关资源
      最近更新 更多