Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding consecutive consonants in a word

Tags:

python

string

I need code that will show me the consecutive consonants in a word. For example, for "concertation" I need to obtain ["c","nc","rt","t","n"].

Here is my code:

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

I manage to find the consonants, but I don't see how to find them consecutively. Can anybody tell me what I need to do?

like image 349
oezlem Avatar asked Jan 02 '15 16:01

oezlem


1 Answers

You can use regular expressions, implemented in the re module

Better solution

>>> re.findall(r'[bcdfghjklmnpqrstvwxyz]+', "concertation", re.IGNORECASE)
['c', 'nc', 'rt', 't', 'n']
  • [bcdfghjklmnprstvyz]+ matches any sequence of one or more characters from the character class

  • re.IGNORECASE enables a case in sensitive match on the characters. That is

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

Another Solution

>>> import re
>>> re.findall(r'[^aeiou]+', "concertation",)
['c', 'nc', 'rt', 't', 'n']
  • [^aeiou] Negated character class. Matches anything character other than the one in this character class. That is in short Matches consonents in the string

  • + quantifer + matches one or more occurence of the pattern in the string

Note This will also find the non alphabetic, adjacent characters in the solution. As the character class is anything other than vowels

Example

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

If you are sure that the input always contain alphabets, this solution is ok


 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. 

If you are curious about how the result is obtained for

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
like image 130
nu11p01n73R Avatar answered Sep 24 '22 09:09

nu11p01n73R