I have a string "42 0" (for example) and need to get an array of the two integers. Can I do a .split on a space?
解决方案
6 answers is not nearly enough for a question that OP could easily have answered himself by testing in the interpreter:
>>> "42 0".split() # or .split(" ")
['42', '0']
A: Yes.
But it has not been specifically pointed out that the split method by default splits on whitespace (space, tab, carriage return and newline) if you do not supply an argument to it.
>>> " \r 42\n\r \t\n \r0\n\r\n".split()
['42', '0']
Also, using map usually looks cleaner than using list comprehensions when you want to convert the items of iterables to built-ins like int, float, str, etc. In Python 2:
>>> map(int, "42 0".split())
[42, 0]
In Python 3, map will return a lazy object, you can get it into a list with list(), or use as is in a for loop for example:
>>> map(int, "42 0".split())
>>> list(map(int, "42 0".split()))
[42, 0]