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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17506163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:21:54  来源:igfitidea点击:

How to convert a boolean array to an int array

pythonintegerbooleantype-conversionscilab

提问by Kwolf

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

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

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

In Scilab I can use:

在 Scilab 中,我可以使用:

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

or even just multiply it by 1:

甚至只是乘以 1:

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

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

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

采纳答案by BrenBarn

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

Numpy 数组有一个astype方法。就做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:

请注意,甚至可能不需要这样做,具体取决于您使用数组的目的。在许多情况下,Bool 会自动提升为 int,因此您可以将其添加到 int 数组中,而无需显式转换它:

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

回答by Sukrit Kalra

The 1*ymethod works in Numpy too:

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]) 

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

如果您需要一种将 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]

Or using list comprehensions:

或者使用列表推导式:

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

回答by cjm

Using numpy, you can do:

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

y = x.astype(int)

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

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

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

回答by bsoist

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

回答by Gioelelm

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)

回答by Thomas G.

A funny way to do this is

一个有趣的方法是

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