I have a Word: HAPPY I want to split the word HAPPY like this {"HA", "AP", "PP", "PY"} using python.
I tried the function:
itertools.combinations("HAPPY", 2)
This finds me all the possible combinations from the word HAPPY, which I don't want. All I want is to find all the transitions between the characters.
I would appraciate any suggestions. Thank you in Advance!
You may use a regex:
import re
s = 'HAPPY'
print(re.findall(r'(?=(..))', s))
// => ['HA', 'AP', 'PP', 'PY']
See the Python demo
The (?=(..)) pattern finds a location followed with any 2 chars other than line break chars and captures these 2 chars. Then, the regex engine steps forward to the next location and grabs two more chars, and so on.
As for performance, if you compile the regex the performance difference is not that big, but comprehension should be a bit faster:
import re
import time
s = 'HAPPY'
rx = re.compile(r'(?=(..))', re.DOTALL)
def test_regex():
return rx.findall(s)
def test_comprehension():
return [(s)[i:i+2] for i in range(0,len(s)-1)]
n = 10000
t0 = time.time()
for i in range(n): test_regex()
t1 = time.time()
print('regex: {}'.format(t1-t0))
t0 = time.time()
for i in range(n): test_comprehension()
t1 = time.time()
print('comprehension: {}'.format(t1-t0))
# => regex: 0.00773191452026
# => comprehension: 0.00626182556152
See the online test
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