在一个单词中找到连续的辅音

时间:2015-01-02 16:03:13

标签: python string

我需要能够向我显示一个单词中连续辅音的代码。例如,对于"concertation",我需要获取["c","nc","rt","t","n"]

这是我的代码:

def SuiteConsonnes(mot):
    consonnes=[]
    for x in mot:
        if x in "bcdfghjklmnprstvyz":
           consonnes += x + ''
    return consonnes

我设法找到辅音,但我不知道如何连续找到它们。谁能告诉我我需要做什么?

8 个答案:

答案 0 :(得分:20)

您可以使用在re module

中实施的正则表达式

更好的解决方案

>>> re.findall(r'[bcdfghjklmnpqrstvwxyz]+', "concertation", re.IGNORECASE)
['c', 'nc', 'rt', 't', 'n']
  • [bcdfghjklmnprstvyz]+匹配字符类中一个或多个字符的任意序列

  • re.IGNORECASE启用字符敏感匹配的案例。那是

    >>> re.findall(r'[bcdfghjklmnpqrstvwxyz]+', "CONCERTATION", re.IGNORECASE)
    ['C', 'NC', 'RT', 'T', 'N']
    

另一种解决方案

>>> import re
>>> re.findall(r'[^aeiou]+', "concertation",)
['c', 'nc', 'rt', 't', 'n']
  • [^aeiou]否定字符类。匹配此角色类中除此之外的任何字符。这就是字符串中的匹配组件

  • + quantfer +匹配字符串中模式的一个或多个出现

注意这也会在解决方案中找到非字母的相邻字符。由于字符类是除元音之外的任何

实施例

>>> re.findall(r'[^aeiou]+', "123concertation",)
['123c', 'nc', 'rt', 't', 'n']

如果您确定输入始终包含字母,那么此解决方案就可以了


 re.findall(pattern, string, flags=0)

    Return all non-overlapping matches of pattern in string, as a list of strings. 
    The string is scanned left-to-right, and matches are returned in the order found. 

如果您对如何获得

的结果感到好奇

re.findall(r'[bcdfghjklmnpqrstvwxyz]+', "concertation")

concertation
|
c

concertation
 |
 # o is not present in the character class. Matching ends here. Adds match, 'c' to ouput list


concertation
  |
  n

concertation
   |
   c


concertation
    |
     # Match ends again. Adds match 'nc' to list 
     # And so on

答案 1 :(得分:11)

您可以使用正则表达式和re模块的split函数执行此操作:

>>> import re
>>> re.split(r"[aeiou]+", "concertation", flags=re.I)
['c', 'nc', 'rt', 't', 'n']

只要一个或多个连续元音匹配,此方法就会分割字符串。

解释正则表达式"[aeiou]+":此处元音已收集到类[aeiou]中,而+表示此类中任何字符的一个或多个匹配项可以匹配。因此,字符串"concertation"分为oeaio

re.I标志意味着将忽略字母的大小写,从而有效地使字符类等于[aAeEiIoOuU]

编辑:要记住的一件事是,此方法隐含地假定该单词仅包含元音和辅音。数字和标点符号将被视为非元音/辅音。要匹配连续辅音,请将re.findall与字符类中列出的辅音一起使用(如其他答案中所述)。

输入所有辅音的一个有用的捷径是使用第三方regex模块而不是re

这个模块支持set操作,所以包含辅音的字符类可以整齐地写成整个字母减去元音:

[[a-z]--[aeiou]] # equal to [bcdefghjklmnpqrstvwxyz]

[a-z]是整个字母表,--设置为差异,[aeiou]为元音。

答案 2 :(得分:9)

如果您正在使用非正则表达式解决方案,itertools.groupby在这里可以正常工作,就像这样

>>> from itertools import groupby
>>> is_vowel = lambda char: char in "aAeEiIoOuU"
>>> def suiteConsonnes(in_str):
...     return ["".join(g) for v, g in groupby(in_str, key=is_vowel) if not v]
... 
>>> suiteConsonnes("concertation")
['c', 'nc', 'rt', 't', 'n']

答案 3 :(得分:3)

一个真正,非常简单的解决方案,无需导入任何东西就是用一个东西替换元音,然后拆分那个东西:

def SuiteConsonnes(mot):
    consonnes = ''.join([l if l not in "aeiou" else "0" for l in mot])
    return [c for c in consonnes.split("0") if c is not '']

为了使它与您的代码非常相似 - 并添加生成器 - 我们得到了这个:

def SuiteConsonnes(mot):
    consonnes=[]
    for x in mot:
        if x in "bcdfghjklmnprstvyz":
            consonnes.append(x)
        elif consonnes:
            yield ''.join(consonnes)
            consonnes = []
    if consonnes: yield ''.join(consonnes)

答案 4 :(得分:2)

def SuiteConsonnes(mot):
    consonnes=[]
    consecutive = '' # initialize consecutive string of consonants
    for x in mot:
        if x in "aeiou":   # checks if x is not a consonant
           if consecutive:  # checks if consecutive string is not empty
              consonnes.append(consecutive)  # append consecutive string to consonnes
              consecutive = ''  # reinitialize consecutive for another consecutive string of consonants
        else:
           consecutive += x   # add x to consecutive string if x is a consonant or not a vowel
    if consecutive: # checks if consecutive string is not empty
        consonnes.append(consecutive)  # append last consecutive string of consonants  
    return consonnes

SuiteConsonnes('concertation')
#['c', 'nc', 'rt', 't', 'n']

答案 5 :(得分:1)

不是我推荐它的可读性,但是单行解决方案是:

In [250]: q = "concertation"
In [251]: [s for s in ''.join([l if l not in 'aeiou' else ' ' for l in q]).split()]
Out[251]: ['c', 'nc', 'rt', 't', 'n']

即:用空格连接非元音,然后在空格上再次分割。

答案 6 :(得分:1)

使用re内置模块的正则表达式:

import re

def find_consonants(string):
    # find all non-vovels occuring 1 or more times: 
    return re.findall(r'[^aeiou]+', string)

答案 7 :(得分:1)

虽然我认为你应该使用@ nu11p01n73R的答案,但这也有效:

re.sub('[AaEeIiOoUu]+',' ','concertation').split()