Convert numpy array to single number

Papp Győző pappgyozo at gmail.com
Thu May 1 09:26:58 EDT 2014


Maybe something like this?
>>> to_num = lambda array: np.sum(array * 2**np.arange(len(array)-1, -1,
-1))
>>> to_num(np.array([1,0,1,0]))
10




2014-04-29 17:42 GMT+02:00 Tom P <werotizy at freent.dd>:

> On 28.04.2014 15:04, mboyd02255 at gmail.com wrote:
>
>> I have a numpy array consisting of 1s and zeros for representing binary
>> numbers:
>>
>> e.g.
>>
>>       >>> binary
>>       array([ 1.,  0.,  1.,  0.])
>>
>> I wish the array to be in the form 1010, so it can be manipulated.
>>
>> I do not want to use built in binary converters as I am trying to build
>> my own.
>>
>>
> Do you mean that each element in the array represents a power of two?
> So array([ 1.,  0.,  1.,  0.]) represents 2^3 + 2 = 6 decimal?
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140501/37874e39/attachment.html>


More information about the Python-list mailing list