Here is two methods of how you can resolve your task.
First method without importing anything and the second using re module with a list of escaped operators:
import re
operators = ['+', '-', '*', '/']
strings = ['1981+198-19871*1981/555']
def split_string(data: list, operators: list):
for elm in data:
out = ''
for k in elm:
if k not in operators:
out += k
else:
yield out
out = ''
if out:
yield out # yield the last element
def re_split_string(data: list, operators: list):
for elm in data:
escaped = ''.join([re.escape(operator) for operator in operators])
if escaped:
pattern = r'[{operators}]'.format(operators=escaped)
yield from re.split(pattern, elm)
else:
yield elm
first = list(split_string(strings, operators))
print(first)
second = list(re_split_string(strings, operators))
print(second)
Output:
['1981', '198', '19871', '1981', '555']
['1981', '198', '19871', '1981', '555']
PS: If you want to see the performance of each method, let's for example use a big string strings = ['1981+198-19871*1981/555' * 1000]
Results in my machine:
In [1]: %timeit split_string(strings, operators)
211 ns ± 0.509 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [2]: %timeit re_split_string(strings, operators)
211 ns ± 0.49 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
As you can see, the two methods have nearly the same execution time.
re.split: doimport reand thenre.split('\W+', '1981+198-19871*1981/555'). That will result in['1981', '198', '19871', '1981', '555'].string(which shouldn't be called this way, since it is actually a list) while looping. In the third loop, you start with a list of length 2, and change it to a list of length 1 after the first iteration.