简体   繁体   English

如何将一串空格分隔的数字拆分为整数?

[英]How to split a string of space separated numbers into integers?

I have a string "42 0" (for example) and need to get an array of the two integers.我有一个字符串"42 0" (例如)并且需要获取两个整数的数组。 Can I do a .split on a space?我可以对空间进行.split吗?

Use str.split() :使用str.split()

>>> "42 0".split()  # or .split(" ")
['42', '0']

Note that str.split(" ") is identical in this case, but would behave differently if there were more than one space in a row.请注意, str.split(" ")在这种情况下是相同的,但如果一行中有多个空格,则行为会有所不同。 As well, .split() splits on all whitespace, not just spaces.同样, .split()在所有空格上拆分,而不仅仅是空格。

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:当您想将可迭代项转换为内置函数(如intfloatstr等)时,使用map通常比使用列表推导式看起来更简洁。在 Python 2 中:

>>> map(int, "42 0".split())
[42, 0]

In Python 3, map will return a lazy object.在 Python 3 中, map将返回一个惰性对象。 You can get it into a list with list() :您可以使用list()将其放入列表中:

>>> map(int, "42 0".split())
<map object at 0x7f92e07f8940>
>>> list(map(int, "42 0".split()))
[42, 0]
text = "42 0"
nums = [int(n) for n in text.split()]
l = (int(x) for x in s.split())

If you are sure there are always two integers you could also do:如果您确定总是有两个整数,您也可以这样做:

a,b = (int(x) for x in s.split())

or if you plan on modifying the array after或者如果您计划在之后修改数组

l = [int(x) for x in s.split()]

这应该有效:

[ int(x) for x in "40 1".split(" ") ]

Of course you can call split , but it will return strings, not integers.当然你可以调用split ,但它会返回字符串,而不是整数。 Do

>>> x, y = "42 0".split()
>>> [int(x), int(y)]
[42, 0]

or或者

[int(x) for x in "42 0".split()]

I have a string "42 0" (for example) and need to get an array of the two integers.我有一个字符串"42 0" (例如),需要获取两个整数的数组。 Can I do a .split on a space?我可以在空格上进行.split吗?

You can split and ensure the substring is a digit in a single line:您可以拆分并确保子字符串是一行中的数字:

In [1]: [int(i) for i in '1 2 3a'.split() if i.isdigit()]
Out[1]: [1, 2]

Other answers already show that you can use split() to get the values into a list .其他答案已经表明您可以使用split()将值放入list If you were asking about Python's arrays , here is one solution:如果您问的是 Python 的arrays ,这是一种解决方案:

import array
s = '42 0'
a = array.array('i')
for n in s.split():
    a.append(int(n))

Edit: A more concise solution:编辑:一个更简洁的解决方案:

import array
s = '42 0'
a = array.array('i', (int(t) for t in s.split()))

Given: text = "42 0"给定: text = "42 0"

import re
numlist = re.findall('\d+',text)

print(numlist)

['42', '0']

I have a string "42 0" (for example) and need to get an array of the two integers.我有一个字符串"42 0" (例如),需要获取两个整数的数组。 Can I do a .split on a space?我可以在空格上进行.split吗?

I have a string "42 0" (for example) and need to get an array of the two integers.我有一个字符串"42 0" (例如),需要获取两个整数的数组。 Can I do a .split on a space?我可以在空格上进行.split吗?

Use numpy's fromstring :使用 numpy 的fromstring

import numpy as np

np.fromstring("42 0", dtype=int, sep=' ')
>>> array([42,  0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM