I basically want the same functionality of preg_match_all()from PHP in a Python way.
If I have a regex pattern and a string, is there a way to search the string and get back a dictionary of each occurrence of a vowel, along with its position in the string?
Example:
s = "supercalifragilisticexpialidocious"
Would return:
{
'u' : 1,
'e' : 3,
'a' : 6,
'i' : 8,
'a' : 11,
'i' : 13,
'i' : 15
}
You can do this faster without regexp
[(x,i) for i,x in enumerate(s) if x in "aeiou"]
Here are some timings:
For s = "supercalifragilisticexpialidocious"
timeit [(m.group(0), m.start()) for m in re.finditer('[aeiou]',s)]
10000 loops, best of 3: 27.5 µs per loop
timeit [(x,i) for i,x in enumerate(s) if x in "aeiou"]
100000 loops, best of 3: 14.4 µs per loop
For s = "supercalifragilisticexpialidocious"*100
timeit [(m.group(0), m.start()) for m in re.finditer('[aeiou]',s)]
100 loops, best of 3: 2.01 ms per loop
timeit [(x,i) for i,x in enumerate(s) if x in "aeiou"]
1000 loops, best of 3: 1.24 ms per loop
What you ask for can't be a dictionary, since it has multiple identical keys. However, you can put it in a list of tuples like this:
>>> [(m.group(0), m.start()) for m in re.finditer('[aeiou]',s)]
[('u', 1), ('e', 3), ('a', 6), ('i', 8), ('a', 11), ('i', 13), ('i', 15), ('i', 18), ('e', 20), ('i', 23), ('a', 24), ('i', 26), ('o', 28), ('i', 30), ('o', 31), ('u', 32)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With