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

Oleksandr Huziy guziy.sasha at gmail.com
Tue Oct 11 13:57:49 EDT 2011


Another recipe:

>>> x = [True, False]
>>> int( (len(x) * '%d') % tuple(x) ,2)
2

--
Oleksandr

2011/10/11 eat <e.antero.tammi at gmail.com>

> Hi
>
> On Tue, Oct 11, 2011 at 8:29 PM, Jacob Biesinger <jake.biesinger at 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 at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at 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/3464625d/attachment.html>


More information about the SciPy-User mailing list