How do i get list of numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

31st Jul 2021, 11:31 AM
Gouri Shinde
Gouri Shinde - avatar
5 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.
31st Jul 2021, 5:37 PM
Lothar
Lothar - avatar
+ 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)
31st Jul 2021, 11:40 AM
Abhay
Abhay - avatar
31st Jul 2021, 7:04 PM
Shadoff
Shadoff - avatar
+ 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])
31st Jul 2021, 12:03 PM
Angelo
Angelo - avatar
+ 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)
31st Jul 2021, 2:26 PM
Solo
Solo - avatar