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

Oleksandr Huziy guziy.sasha@gmail....
Tue Oct 11 12:57:49 CDT 2011


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
>> Graduate Student
>> 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
>
>
-------------- 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