如何将字符串分割成在Python两个整数 [英] How to split a string into two integers in python
问题描述
我有一个字符串42 0(例如),并且需要进入一个阵列的两个整数。我可以在一个空间做一个.split?
I have a string "42 0" (for example) and need to enter into an array the two integers. can i do a .split on a space?
在此先感谢
推荐答案
6的答案是远远不够的为OP可以很容易地通过试验的的间preTER :
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']
答:是的,
但它没有被明确指出,拆分
通过在空白默认分裂(空格,制表符的回车的和的换行符)如果你不提供的参数吧。
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']
此外,使用地图
当你想iterables的项目内建转换如 INT
,浮动
, STR
等。
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.:
>>> map(int, "42 0".split())
[42, 0]
这篇关于如何将字符串分割成在Python两个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!