How to extract numbers from a string that has no spaces into a list
Question:
I have an assignment for which my script should be able to receive a string for input (e.g. "c27bdj3jddj45g"
) and extract the numbers into a list (not just the digits, it should be able to detect full numbers).
I am not allowed to use regex at all, only simple methods like split, count and append.
Any ideas? (Using python)
Example for the output needed for the string I gave as an example:
['27','3', '45']
Nothing I have tried so far is worth mentioning here, I am pretty lost on which approach to take here without re.findall
, which I cannot use.
Answers:
s='c27bdj3jddj45g'
lst=[]
for x in s:
if x.isdigit():
lst.append(x)
else:
lst.append('$') # here $ is appended as a place holder so that all the numbers can come togetrher
Now, lst
becomes :
#['$', '2', '7', '$', '$', '$', '3', '$', '$', '$', '$', '4', '5', '$']
''.join(lst).split('$')
becomes:
['', '27', '', '', '3', '', '', '', '45', '']
Finally doing list comprehension to extract the numbers:
[x for x in ''.join(lst).split('$') if x.isdigit()]
['27', '3', '45']
You can do this with a for-loop
and save the numbers
. Then, when you see no digit, append digits and reset the string.
s = 'g38ff11'
prv = ''
res = []
for c in s:
if c.isdigit():
prv += c
else:
if prv != '': res.append(prv)
prv = ''
if prv != '': res.append(prv)
print(res)
Output:
['38', '11']
You can also write a lambda to check and append:
s = 'g38ff11'
prv = ''
res = []
append_dgt = lambda prv, res: res.append(prv) if prv!="" else None
for c in s:
if c.isdigit():
prv += c
else:
append_dgt(prv, res)
prv = ''
append_dgt(prv, res)
print(res)
One way to solve it is to use the groupby from itertools lib:
from itertools import groupby
s = 'c27bdj3jdj45g11' # last dight is 11
ans = []
for k, g in groupby(s, lambda x: x.isdigit()):
if k: # True if x is digit
ans.append(''.join(g))
ans
['27', '3', '45', '11']
Second solution – even OP has opt out the regex, but this is just for a reference. (to show how much easier to approach this type of puzzle – which should be the way to go)
You could try to use regex – re lib like this (if there’s no restriction!)
s = 'c27bdj3jddj45g'
import re
list(re.findall(r'd+', s)) # matching one more digits
['27', '3', '45']
# or to get *integer*
list(map(int, re.findall(r'd+', s)))
[27, 3, 45]
string=’c27bdj3jddj45g’
lst=[]
for i in string:
if i.isdigit():
lst.append(i)
else:
lst.append('$')
print([int(i) for i in ''.join(lst).split('$') if i.isdigit()])
I have an assignment for which my script should be able to receive a string for input (e.g. "c27bdj3jddj45g"
) and extract the numbers into a list (not just the digits, it should be able to detect full numbers).
I am not allowed to use regex at all, only simple methods like split, count and append.
Any ideas? (Using python)
Example for the output needed for the string I gave as an example:
['27','3', '45']
Nothing I have tried so far is worth mentioning here, I am pretty lost on which approach to take here without re.findall
, which I cannot use.
s='c27bdj3jddj45g'
lst=[]
for x in s:
if x.isdigit():
lst.append(x)
else:
lst.append('$') # here $ is appended as a place holder so that all the numbers can come togetrher
Now, lst
becomes :
#['$', '2', '7', '$', '$', '$', '3', '$', '$', '$', '$', '4', '5', '$']
''.join(lst).split('$')
becomes:
['', '27', '', '', '3', '', '', '', '45', '']
Finally doing list comprehension to extract the numbers:
[x for x in ''.join(lst).split('$') if x.isdigit()]
['27', '3', '45']
You can do this with a for-loop
and save the numbers
. Then, when you see no digit, append digits and reset the string.
s = 'g38ff11'
prv = ''
res = []
for c in s:
if c.isdigit():
prv += c
else:
if prv != '': res.append(prv)
prv = ''
if prv != '': res.append(prv)
print(res)
Output:
['38', '11']
You can also write a lambda to check and append:
s = 'g38ff11'
prv = ''
res = []
append_dgt = lambda prv, res: res.append(prv) if prv!="" else None
for c in s:
if c.isdigit():
prv += c
else:
append_dgt(prv, res)
prv = ''
append_dgt(prv, res)
print(res)
One way to solve it is to use the groupby from itertools lib:
from itertools import groupby
s = 'c27bdj3jdj45g11' # last dight is 11
ans = []
for k, g in groupby(s, lambda x: x.isdigit()):
if k: # True if x is digit
ans.append(''.join(g))
ans
['27', '3', '45', '11']
Second solution – even OP has opt out the regex, but this is just for a reference. (to show how much easier to approach this type of puzzle – which should be the way to go)
You could try to use regex – re lib like this (if there’s no restriction!)
s = 'c27bdj3jddj45g'
import re
list(re.findall(r'd+', s)) # matching one more digits
['27', '3', '45']
# or to get *integer*
list(map(int, re.findall(r'd+', s)))
[27, 3, 45]
string=’c27bdj3jddj45g’
lst=[]
for i in string:
if i.isdigit():
lst.append(i)
else:
lst.append('$')
print([int(i) for i in ''.join(lst).split('$') if i.isdigit()])