开发者

String to list without using regex

开发者 https://www.devze.com 2023-03-14 21:18 出处:网络
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

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

精彩评论

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