且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中将没有空格的字符串拆分为整数列表?

更新时间:2022-11-14 16:22:00

这里不需要使用 split:

>>>a = "12345">>>地图(INT,一)[1, 2, 3, 4, 5]

字符串也是可迭代的

对于 python 3x:

list(map(int, a))

I take a string of integers as input and there are no spaces or any kind of separator:

12345

Now I want this string to converted into a list of individual digits

[1,2,3,4,5]

I've tried both

numlist = map(int,input().split(""))

and

numlist = map(int,input().split(""))

Both of them give me Empty Separator error. Is there any other function to perform this task?

You don't need to use split here:

>>> a = "12345"    
>>> map(int, a)
[1, 2, 3, 4, 5]

Strings are Iterable too

For python 3x:

list(map(int, a))