Possible Duplicate:
Python: How do I convert an array of strings to an array of numbers?
I am trying to map a list of strings to Int, and when I map the Int function it is splitting 2 digit numbers into 2 item lists:
I have the following code:
>>> MyList
['10', '5', '6', '7', '8', '9']
>>> MyList = [map(int,x) for x in MyList]
>>> MyList
[[1, 0], [5], [6], [7], [8], [9]]
What is the correct way to get a list that looks like this:
[10, 5, 6, 7, 8, 9]
Use
map(int, ...directly on the list:or, in most circumstances, use a list comprehension:
but not both.
mapapplies the function to every item in the iterable by itself — that is its purpose.Similarly, a list comprehension runs the expression on every item in the iterable, so you don’t need
map, just the expression you want to run every time.