the separate number from the string, but when successive '1', separate them
I think there must have a smart way to solve the question.
s = 'NNNN1234N11N1N123'
expected result is:
['1234','1','1','1','123']
the separate number from the string, but when successive '1', separate them
I think there must have a smart way to solve the question.
s = 'NNNN1234N11N1N123'
expected result is:
['1234','1','1','1','123']
I think what you want can be solved by using the re
module
>>> import re
>>> re.findall('(?:1[2-90]+)|1', 'NNNN1234N11N1N123')
EDIT: As suggested in the comments by @CrafterKolyan the regular expression can be reduced to 1[2-90]*
.
Outputs
['1234', '1', '1', '1', '123']
I also would use regular expressions (re
module), but other function, namely re.split
following way:
import re
s = 'NNNN1234N11N1N123'
output = re.split(r'[^\d]+|(?<=1)(?=1)',s)
print(output) # ['', '1234', '1', '1', '1', '123']
output = [i for i in output if i] # jettison empty strs
print(output) # ['1234', '1', '1', '1', '123']
Explanation: You want to split str
to get list
of str
s - that is for what re.split
is used. First argument of re.split
is used to tell where split should happen, with everything which will be matched being removed if capturing groups are not used (similar to str
method split
), so I need to specify two places where cut happen, so I used |
that is alternative and informed re.split
to cut at:
[^\d]+
that is 1 or more non-digits(?<=1)(?=1)
that is empty str
preceded by 1
and followed by 1
, here I used feature named zero-length assertion (twice)Note that re.split
produced ''
(empty str
) before your desired output - this mean that first cut (NNNN
in this case) spanned from start of str
. This is expected behavior of re.split
although we do not need that information in this case so we can jettison any empty str
s, for which I used list
comprehension.