如何将字符串分割成在Python两个整数 [英] How to split a string into two integers in python

查看:275
本文介绍了如何将字符串分割成在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆