+ 2
How do i get list of numbers?
a=("9,223;789:52 88:65;2,78:55") b= "" for char in a: if not char.isnumeric(): b=b+char print(b) for char in a: if char not in b: value="".join(char).split() else: " " for val in value: print(int(val)) i wrote this code.,but i didn't get my desire output. Answer should be=[9,223,789,52,88,65,2,78,55] what should i do?
6 Answers
+ 6
Gouri Shinde ,
i have done a try with regex split function. it allows to define multiple separators:
import re
a=("9,223;789:52 88:65;2,78:55")
print([int(num) for num in re.split(';|,|:|\s',a)])
result is: [9, 223, 789, 52, 88, 65, 2, 78, 55]
>>> [edited]:
âȘïžIf the list contains several consecutive identical separators, the above mentioned code will create an error. to fix this, the pattern string has to be modified.
import re
a=("9,223;789::::52 88:65;2,,,,78:55")
print([int(num) for num in re.split(';+|,+|:+|\s+',a)])
this will create a correct result. it is done by adding a '+', to each separator. that means that it searches for "1 or more" of the respective preceding characters.
+ 5
import re
a=("9,223;789:52 88:65;2,78:55")
b=re.sub("[;: ]",",",a)
c=b.split(",")
d=list(map(lambda x:int(x),c))
print(d)
+ 3
How about this solution?
https://code.sololearn.com/cDkrNy2tg77E/?ref=app
+ 2
lines 7 to 13, judging by what you wrote I think you originally meant this:
"".join([char if char not in b else " " for char in a]).split()
print([int(val) for val in value])
+ 2
a=("9,223;789:52 88:65;2,78:55")
b = value = " "
be=[]
for char in a:
if not char.isnumeric():
b=b+char
if char not in b:
value += char
else:
value += " "
#or:
"""
for char in a:
if not char.isnumeric():
b += char
value += char if char not in b else" "
"""
for val in value.split():
be += [int(val)]
print(be)