How can I find consonants letters that came after the vowels in words of string and count the frequency
str = 'car regular double bad '
result19 = re.findall(r'\b\w*[aeiou][^ aeiou]\w*\b' , str)
print(result19) #doesn't work
Expected output
letter r count = 2
letter b count = 1
letter d count = 1
CodePudding user response:
I am not sure whether this is what you want or not, but it might help as an answer and not a comment.
I think you are on the right track, but you need a few modifications and other lines to achieve the excepted:
import re
myStr = 'car regular double bad '
result19 = re.findall(r'[aeiou][^aeiou\s] ' , myStr)
myDict = {}
for value in result19:
if not value[1] in myDict:
myDict[value[1]] = 0
myDict[value[1]] = 1
myDict
This will result in a dictionary containing the values and the number the have appeared:
{'b': 1, 'd': 1, 'g': 1, 'l': 1, 'r': 2}
For having a better output you can use a for loop to print each key and its value:
for chr, value in myDict.items():
print(chr, "->", value)
Output
r -> 2
g -> 1
l -> 1
b -> 1
d -> 1
CodePudding user response:
Your pattern \b\w*[aeiou][^ aeiou]\w*\b
matches zero or more repetitions of a word character using \w*
and only matches a single occurrence of [aeiou][^ aeiou]
in the "word"
If you want to match all consonant letters based on the alphabet a-z after a vowel, you can match a single occurrence of [aeiou]
and use a capture group matching a single consonant.
Then make use of re.findall to return a list of the group values.
import re
txt = 'car regular double bad '
lst = re.findall(r'[aeiou]([b-df-hj-np-tv-z])', txt)
dct = {c: lst.count(c) for c in lst}
print(dct)
Output
{'r': 2, 'g': 1, 'l': 1, 'b': 1, 'd': 1}
If you want to match a non whitespace char other than a vowel after matching a vowel, you can use this pattern [aeiou]([^\saeiou])
Note that the l
is also in the output as it comes after the u
in ul