Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'elegant': 0.07; 'element': 0.07; 'string': 0.09; '[1,': 0.09; 'bits': 0.09; 'bytes,': 0.09; 'integers': 0.09; 'part,': 0.09; 'python': 0.11; '105,': 0.16; '24,': 0.16; 'entry:': 0.16; 'integers.': 0.16; 'iterators': 0.16; 'threads.': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; '(the': 0.22; 'example': 0.22; 'to:name:python- list@python.org': 0.22; 'byte': 0.24; 'bytes': 0.24; 'integer': 0.24; 'interpret': 0.24; 'skip:l 30': 0.24; 'string,': 0.24; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'received:172.16': 0.29; "i'm": 0.30; 'too.': 0.31; '25,': 0.31; '>>>>': 0.31; 'front': 0.32; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'next': 0.36; 'list': 0.37; 'starting': 0.37; 'message-id:@gmail.com': 0.38; 'to:addr :python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'is.': 0.60; 'liked': 0.60; 'first': 0.61; "you'll": 0.62; 'header :Message-Id:1': 0.63; 'love': 0.65; 'series': 0.66; 'mar': 0.68; 'containing': 0.69; 'hand': 0.80; '2015': 0.84; 'end.': 0.84; 'treating': 0.84; 'utmost': 0.84; 'subject:looking': 0.91; 'fun!': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to; bh=yED+4P/VghIXNjA8K1ElYP7mxVqZfFszJYPMfQh3j4w=; b=umAYulNVphJVPv+3hZrReMseFcqIqeiOzfDpkDLFo4xcG1RWqN1ODUsuEmTfWQ9FKV t2TFHhWPgA6MGF0+H4wwZLvTM+pMeFYxHybJ4NCaOPZIv8A9SEkuAH6NqqMwx0+7Vg7O dDFGFIO3R5ghlKRYS6EJYe+Bw1pS9zqIZi8+tJgQmt6XfEGPI9fY8P85Qf6nyGGL2L9n Ycy4U31dPEg9gGNFuwJHAKet/9+3hgcgnn1oHy70hIFR8ZdDzx6FTYEYV/g6MhbBUWFL bRVb0GIwgwQ6sxpEDXlnE8GWFrRyQ92s7L6hI87KEFAMWs3MfVOVIaE59uYTsW0x9Kn3 gRXQ== X-Received: by 10.70.1.227 with SMTP id 3mr19656338pdp.110.1427309399149; Wed, 25 Mar 2015 11:49:59 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) Subject: Re: Newbie looking for elegant solution From: Travis Griggs In-Reply-To: Date: Wed, 25 Mar 2015 11:49:56 -0700 Content-Transfer-Encoding: quoted-printable References: To: "python-list@python.org" X-Mailer: Apple Mail (2.2070.6) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427309403 news.xs4all.nl 2905 [2001:888:2000:d::a6]:34181 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87964 > On Mar 24, 2015, at 8:28 PM, Chris Angelico wrote: >=20 > On Wed, Mar 25, 2015 at 2:13 PM, wrote: >> I have a list containing 9600 integer elements - each integer is = either 0 or 1. >>=20 >> Starting at the front of the list, I need to combine 8 list elements = into 1 by treating them as if they were bits of one byte with 1 and 0 = denoting bit on/off (the 8th element would be the rightmost bit of the = first byte). >>=20 >> Speed is not of utmost importance - an elegant solution is. Any = suggestions? >=20 > Oooh fun! >=20 >>>> l =3D [1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, = 0, 1, 0, 1] >>>> list(int(''.join(str(i) for i in l),2).to_bytes(len(l)//8,'big')) > [177, 105, 117] >=20 > Convert it into a string, convert the string to an integer > (interpreting it as binary), then convert the integer into a series of > bytes, and interpret those bytes as a list of integers. >=20 > Example works in Python 3. For Python 2, you'll need ord() to get the > integers at the end. >=20 > I'm not sure how elegant this is, but it's a fun trick to play with :) >=20 > Next idea please! I love these kinds of threads. Me too. These are my favorite threads. Here=E2=80=99s my entry: [sum(b << (7 - i) for i, b in enumerate(bits)) for bits in zip(*[l[n::8] = for n in range(8)])] I think there has to be a better way to do the left hand part, but I = liked the zipped iterators on 8 slices.