开发者

String to list without using regex

If I have a string as in

mystring = "1212, 1215, 2132, 2312, 3333"

What is the recommended approach to convert it into a list? as in

mylist = [1212, 1215, 2132,开发者_开发问答 2312, 3333]

Please note that mystring can assume a single value too, as in mystring = "1212"


map(int, mystring.split(','))

That's it.


List comprehension syntax:

[int(n) for n in mystring.split(',')]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜