Created
May 12, 2017 17:53
-
-
Save kwikwag/36a681416321919a3cd2d2feaffeddb6 to your computer and use it in GitHub Desktop.
Python: splitting a string into two parts at the nth delimiter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from timeit import timeit | |
import re | |
def a(s, delim, n): | |
''' | |
Two-param split, retaining head | |
''' | |
r = s.split(delim, n)[n] | |
return s[:-len(r)-len(delim)], r | |
def b(s, delim, n): | |
''' | |
Iterative find, retaining head | |
''' | |
index = -1 | |
while n > 0: | |
index = s.find(delim, index+1) | |
if index < 0: | |
break | |
n -= 1 | |
return s[:index], s[index+1:] | |
_re_cache1 = dict() | |
def c(s, c, n): | |
''' | |
Regular expressions, retaining head | |
''' | |
r = _re_cache1.get(c, None) | |
if r is None: | |
_re_cache1[c] = r = re.compile(br'^((?:[^%c]*%c){%d}[^%c]*)%c(.*)' % (c,c,n-1,c,c)) | |
l = () | |
m = r.match(s) | |
if m: | |
return m.groups() | |
def d(s, delim, n): | |
''' | |
Two-param split, discarding head | |
''' | |
return s.split(delim, n)[n] | |
def e(s, delim, n): | |
''' | |
Iterative find, discarding head | |
''' | |
index = -1 | |
while n > 0: | |
index = s.find(delim, index+1) | |
if index < 0: | |
break | |
n -= 1 | |
return s[index+1:] | |
_re_cache2 = dict() | |
def f(s, c, n): | |
''' | |
Regular expressions, discarding head | |
''' | |
r = _re_cache2.get(c, None) | |
if r is None: | |
_re_cache2[c] = r = re.compile(br'^((?:[^%c]*%c){%d}[^%c]*)%c(.*)' % (c,c,n-1,c,c)) | |
l = () | |
m = r.match(s) | |
if m: | |
return m.groups()[1] | |
for x in [a,b,c,d,e,f]: | |
print(x.__doc__) | |
print(x(b"abc:def:efg:hij:klm", b":", 2)) | |
print(timeit(x.__name__ + '(b"abc:def:efg", b":", 2)', setup='from __main__ import ' + x.__name__, number=10**7)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment