# [SciPy-User] From 1-D boolean array to integer index

Alok Singhal alok@merfinllc....
Tue Oct 11 13:30:10 CDT 2011

```Another:

np.sum((1 << np.arange(data.size))[data])

On Tue, Oct 11, 2011 at 10:57 AM, Oleksandr Huziy <guziy.sasha@gmail.com> wrote:
> Another recipe:
>
>>>> x = [True, False]
>>>> int( (len(x) * '%d') % tuple(x) ,2)
> 2
>
> --
> Oleksandr
>
> 2011/10/11 eat <e.antero.tammi@gmail.com>
>>
>> Hi
>>
>> On Tue, Oct 11, 2011 at 8:29 PM, Jacob Biesinger
>> <jake.biesinger@gmail.com> wrote:
>>>
>>> Hi all!
>>> This seems a trivial question, but I couldn't find it in the archives.  I
>>> have a 1-D bool array which I'd like to convert to an integer index.  The
>>> best I've come up with is:
>>> >>> int(''.join(['1' if e else '0' for e in sp.array([True, False])]), 2)
>>> 2
>>> >>> int(''.join(['1' if e else '0' for e in sp.array([True, False,
>>> >>> True])]), 2)
>>> 5
>>> Is there an easier way to do this?
>>
>> Perhaps something like this
>> (np.array([True, False, True])* (2** np.arange(3))).sum()
>> Just my 2 cents,
>> eat
>>>
>>> Thanks!
>>> --
>>> Jake Biesinger
>>> Xie Lab, UC Irvine
>>>
>>>
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>

```