In a code I'm trying to write, I want to convert a string which contains numbers to a list where each element is a number of that string.
I tried to use sub
, a re
function, but I don't have the result I want in a particular case.
x = "8 1 2 9 12"
liste= []
final = []
for s in x:
liste.append(re.sub('\s+',"", s))
for element in liste:
if element =="":
liste.remove("")
for b in liste:
if b != 'X':
final += [int(b)]
else:
final+=["X"]
I expect the output of [8,1,2,9,12]
, but the actual output is [8,1,2,9,1,2]
.