Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'represents': 0.05; 'binary': 0.07; 'element': 0.07; 'tom': 0.07; 'subject:number': 0.09; '-1,': 0.16; 'lambda': 0.16; 'numpy': 0.16; 'subject:Convert': 0.16; 'subject:array': 0.16; 'zeros': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'this?': 0.23; '>>>': 0.24; 'own.': 0.24; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'consisting': 0.31; 'url:python': 0.33; 'maybe': 0.34; 'something': 0.35; 'received:google.com': 0.35; 'representing': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'skip:2 20': 0.60; 'from:charset:utf-8': 0.61; 'email addr:gmail.com': 0.63; 'wish': 0.70; 'power': 0.76; 'converters': 0.84; 'numbers:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=/zLZU4FRbYxPtxM9Uhc3824bY0CJkayKUaVxFFa4/KA=; b=aU7GcFViR+UWNY7JznLLTVMhWrav1UX7fF1Lty9oh2xGwhw0+Wy0Sd6JG7/1gQPc7U Ys++kdQDoBIPjPO1woUye44Dygsy3CyuLt+BSLHZhk+ngp0aZLYISvSmkXAmiU8znneh JgMBDYjLaiuwp0evSoILMU7PZ3xrHVSSMq7ecuqBcV+wbk/QVRr6JUrHzYMGA2/1ChAf m+BRgOyE23XrAp0hAC+zBt9/YF9MAUQTUAJSqWDJKMHaUsVhm4UFwocWfWmxi6wI4tbF RuFVcATV74fv39Me2qpWIy4LY8pRBw9Xb7e2cZ4bsvxtewUhEn2PLnrjifJ8YvA3w6iX ccMw== X-Received: by 10.66.221.4 with SMTP id qa4mr21435433pac.138.1398950838637; Thu, 01 May 2014 06:27:18 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <088cdf75-f196-404b-ba77-567559f431d8@googlegroups.com> From: =?UTF-8?B?UGFwcCBHecWResWR?= Date: Thu, 1 May 2014 15:26:58 +0200 Subject: Re: Convert numpy array to single number To: python-list@python.org Content-Type: multipart/alternative; boundary=047d7b5d472ccb977404f8569f61 X-Mailman-Approved-At: Thu, 01 May 2014 20:51:11 +0200 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398970272 news.xs4all.nl 2934 [2001:888:2000:d::a6]:48525 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70819 --047d7b5d472ccb977404f8569f61 Content-Type: text/plain; charset=UTF-8 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, mboyd02255@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 > --047d7b5d472ccb977404f8569f61 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Maybe something like this?
>>> to_num =3D lamb= da 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 <wero= tizy@freent.dd>:
On 28.04.2014 15:04, mboyd02255@gmail.com wrote:
I have a numpy array consisting of 1s and zeros for representing binary num= bers:

e.g.

=C2=A0 =C2=A0 =C2=A0 >>> binary
=C2=A0 =C2=A0 =C2=A0 array([ 1., =C2=A00., =C2=A01., =C2=A00.])

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., =C2=A00., =C2=A01., =C2=A00.]) represents 2^3 + 2 =3D 6 deci= mal?

--
https://mail.python.org/mailman/listinfo/python-list

--047d7b5d472ccb977404f8569f61--