Convert numpy array to single number

Discussion in 'Python' started by mboyd02255@gmail.com, Apr 28, 2014.

  1. Guest

    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.
    , Apr 28, 2014
    #1
    1. Advertising

  2. On Mon, 28 Apr 2014 06:04:02 -0700, mboyd02255 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.


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



    --
    Steven D'Aprano
    http://import-that.dreamwidth.org/
    Steven D'Aprano, Apr 28, 2014
    #2
    1. Advertising

  3. Dave Angel Guest

    Wrote in message:
    > 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.
    >


    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.

    --
    DaveA
    Dave Angel, Apr 28, 2014
    #3
  4. Tom P Guest

    On 28.04.2014 15:04, 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?
    Tom P, Apr 29, 2014
    #4
  5. 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 <>:

    > On 28.04.2014 15:04, 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
    >
    Papp Győző, May 1, 2014
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drife
    Replies:
    1
    Views:
    360
    Travis E. Oliphant
    Mar 1, 2006
  2. Duncan Smith
    Replies:
    3
    Views:
    417
    Duncan Smith
    Apr 25, 2007
  3. Replies:
    2
    Views:
    486
    Robert Kern
    Nov 13, 2007
  4. W. eWatson
    Replies:
    2
    Views:
    932
    W. eWatson
    Nov 23, 2009
  5. Wanderer
    Replies:
    1
    Views:
    585
    Wanderer
    Mar 24, 2011
Loading...

Share This Page