如何将布尔数组转换为int数组

问题:如何将布尔数组转换为int数组

我使用Scilab,并希望将布尔数组转换为整数数组:

>>> x = np.array([4, 3, 2, 1])
>>> y = 2 >= x
>>> y
array([False, False,  True,  True], dtype=bool)

在Scilab中,我可以使用:

>>> bool2s(y)
0.    0.    1.    1.  

甚至只是将其乘以1:

>>> 1*y
0.    0.    1.    1.  

在Python中是否有一个简单的命令,还是我必须使用循环?

I use Scilab, and want to convert an array of booleans into an array of integers:

>>> x = np.array([4, 3, 2, 1])
>>> y = 2 >= x
>>> y
array([False, False,  True,  True], dtype=bool)

In Scilab I can use:

>>> bool2s(y)
0.    0.    1.    1.  

or even just multiply it by 1:

>>> 1*y
0.    0.    1.    1.  

Is there a simple command for this in Python, or would I have to use a loop?


回答 0

numpy数组有一个astype方法。做吧y.astype(int)

请注意,根据您使用数组的目的,甚至可能没有必要执行此操作。在许多情况下,Bool会自动提升为int,因此您可以将其添加到int数组中,而无需显式转换它:

>>> x
array([ True, False,  True], dtype=bool)
>>> x + [1, 2, 3]
array([2, 2, 4])

Numpy arrays have an astype method. Just do y.astype(int).

Note that it might not even be necessary to do this, depending on what you’re using the array for. Bool will be autopromoted to int in many cases, so you can add it to int arrays without having to explicitly convert it:

>>> x
array([ True, False,  True], dtype=bool)
>>> x + [1, 2, 3]
array([2, 2, 4])

回答 1

1*y方法也适用于Numpy:

>>> import numpy as np
>>> x = np.array([4, 3, 2, 1])
>>> y = 2 >= x
>>> y
array([False, False,  True,  True], dtype=bool)
>>> 1*y                      # Method 1
array([0, 0, 1, 1])
>>> y.astype(int)            # Method 2
array([0, 0, 1, 1]) 

如果您正在寻求一种将Python列表从Boolean转换为int的方法,则可以使用以下map方法:

>>> testList = [False, False,  True,  True]
>>> map(lambda x: 1 if x else 0, testList)
[0, 0, 1, 1]
>>> map(int, testList)
[0, 0, 1, 1]

或使用列表推导:

>>> testList
[False, False, True, True]
>>> [int(elem) for elem in testList]
[0, 0, 1, 1]

The 1*y method works in Numpy too:

>>> import numpy as np
>>> x = np.array([4, 3, 2, 1])
>>> y = 2 >= x
>>> y
array([False, False,  True,  True], dtype=bool)
>>> 1*y                      # Method 1
array([0, 0, 1, 1])
>>> y.astype(int)            # Method 2
array([0, 0, 1, 1]) 

If you are asking for a way to convert Python lists from Boolean to int, you can use map to do it:

>>> testList = [False, False,  True,  True]
>>> map(lambda x: 1 if x else 0, testList)
[0, 0, 1, 1]
>>> map(int, testList)
[0, 0, 1, 1]

Or using list comprehensions:

>>> testList
[False, False, True, True]
>>> [int(elem) for elem in testList]
[0, 0, 1, 1]

回答 2

使用numpy,您可以执行以下操作:

y = x.astype(int)

如果您使用的是非numpy数组,则可以使用列表推导

y = [int(val) for val in x]

Using numpy, you can do:

y = x.astype(int)

If you were using a non-numpy array, you could use a list comprehension:

y = [int(val) for val in x]

回答 3

大多数时候,您不需要转换:

>>>array([True,True,False,False]) + array([1,2,3,4])
array([2, 3, 3, 4])

正确的方法是:

yourArray.astype(int)

要么

yourArray.astype(float)

Most of the time you don’t need conversion:

>>>array([True,True,False,False]) + array([1,2,3,4])
array([2, 3, 3, 4])

The right way to do it is:

yourArray.astype(int)

or

yourArray.astype(float)

回答 4

我知道您要求使用非循环解决方案,但无论如何,我能想到的唯一解决方案可能是内部循环的:

map(int,y)

要么:

[i*1 for i in y]

要么:

import numpy
y=numpy.array(y)
y*1

I know you asked for non-looping solutions, but the only solutions I can come up with probably loop internally anyway:

map(int,y)

or:

[i*1 for i in y]

or:

import numpy
y=numpy.array(y)
y*1

回答 5

一个有趣的方法是

>>> np.array([True, False, False]) + 0 
np.array([1, 0, 0])

A funny way to do this is

>>> np.array([True, False, False]) + 0 
np.array([1, 0, 0])