[英]Python - How do I convert a a string containing more than one number to an int?
How can I convert a string containing more than one number to an int in python, for example: "100, 200, 300"
and "400, 500"
. 如何将包含多个数字的字符串转换为python中的int,例如:
"100, 200, 300"
和"400, 500"
。 I know how I can convert a string containing a single number such as "100"
or "56"
to an int but not a string which contains 2 numbers. 我知道如何将包含单个数字(例如
"100"
或"56"
的字符串转换为int,但不能将包含2个数字的字符串转换为int。
Does anyone know how this could be done? 有谁知道该怎么做?
Thank you so much for your help. 非常感谢你的帮助。
You can also use list comprehensions : 您还可以使用列表推导 :
In [635]: x = "100, 200, 300"
In [638]: results = [int(i) for i in x.split(',')]
In [639]: results
Out[639]: [100, 200, 300]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.