How can I convert a string containing more than one number to an int in python, for example: "100, 200, 300"
and "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.
Does anyone know how this could be done?
Thank you so much for your help.
Using map() for mapping list element to int
x = "100, 200, 300"
list(map(int, x.split(",")))
Output:
[100, 200, 300]
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.