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

eat e.antero.tammi@gmail....
Tue Oct 11 12:49:23 CDT 2011


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
> Graduate Student
> Xie Lab, UC Irvine
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20111011/7bba88ac/attachment.html 


More information about the SciPy-User mailing list