I have this implementation of the split algorithm that different from .split()
method you can use with multiple delimiters. Is this a good way of implementing it (more performance)?
def split(str, delim=" "):
index = 0
string = ""
array = []
while index < len(str):
if str[index] not in delim:
string += str[index]
else:
if string:
array.append(string)
string = ""
index += 1
if string: array.append(string)
return array
Using the standard .split()
method:
>>> print "hello = 20".split()
['hello', '=', '20']
>>> print "one;two; abc; b ".split(";")
['one', 'two', ' abc', ' b ']
Using my implementation:
>>> print split("hello = 20")
['hello', '=', '20']
>>> print split("one;two; abc; b ", ";")
['one', 'two', ' abc', ' b ']
Multiple delimiters:
>>> print split("one;two; abc; b.e. b eeeeee.e.e;;e ;.", " .;")
['one', 'two', 'abc', 'b', 'e', 'b', 'eeeeee', 'e', 'e', 'e']
>>> print split("foo barfoo;bar;foo bar.foo", " .;")
['foo', 'barfoo', 'bar', 'foo', 'bar', 'foo']
>>> print split("foo*bar*foo.foo bar;", "*.")
['foo', 'bar', 'foo', 'foo bar;']
Obs: We can do something like using re.split()
.