# integer - python: 将布尔数组转换为int数组

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

``````

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

``````

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

``````

Numpy数组有一个`astype`方法 ， 只执行 `y.astype(int)`

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

``````

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

``````

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

``````

``` ```
map(int,y)

``` ```

``````
[i*1 for i in y]

``````

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

``````

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

``````

``````
yourArray.astype(int)

``````

``````
yourArray.astype(float)

``````

`y = x.astype(int)`

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