如何在Python中将没有空格的字符串拆分为整数列表? [英] How to split string without spaces into list of integers in Python?

查看:53
本文介绍了如何在Python中将没有空格的字符串拆分为整数列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一串整数作为输入,并且没有空格或任何类型的分隔符:

12345

现在我想将此字符串转换为单个数字的列表

[1,2,3,4,5]

我都试过了

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

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

它们都给我 Empty Separator 错误.是否还有其他功能可以执行此任务?

解决方案

这里不需要使用 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))

这篇关于如何在Python中将没有空格的字符串拆分为整数列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆