Convert numpy array to single number

M

mboyd02255

I have a numpy array consisting of 1s and zeros for representing binary numbers:

e.g.
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.
 
S

Steven D'Aprano

I have a numpy array consisting of 1s and zeros for representing binary
numbers:

e.g.
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.

Did you have a question, or are you just sharing?
 
D

Dave Angel

I have a numpy array consisting of 1s and zeros for representing binary numbers:

e.g.
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.

One thousand and ten is an int, not an array.

So is 10, but it seems a more likely value you might be trying for.

As for using the builtin binary converters, you'd better be a lot
more specific, as you cannot even print an int in decimal form
without utilizing them. The number is in binary already.


Please copy the exact problem and constraints, as well as what
you've written so far and what's wrong with it.
 
T

Tom P

I have a numpy array consisting of 1s and zeros for representing binary numbers:

e.g.
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?
 
P

Papp Győző

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 said:
I have a numpy array consisting of 1s and zeros for representing binary
numbers:

e.g.
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top