Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'elegant': 0.07; 'element': 0.07; 'string': 0.09; '[1,': 0.09; 'bits': 0.09; 'bytes,': 0.09; 'integers': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '105,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integers.': 0.16; 'threads.': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; '(the': 0.22; '>>>': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'byte': 0.24; 'bytes': 0.24; 'integer': 0.24; 'interpret': 0.24; 'skip:l 30': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'idea': 0.28; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '25,': 0.31; 'front': 0.32; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'next': 0.36; 'list': 0.37; 'starting': 0.37; 'list,': 0.38; 'pm,': 0.38; 'sure': 0.39; 'either': 0.39; 'how': 0.40; 'is.': 0.60; 'first': 0.61; "you'll": 0.62; 'love': 0.65; 'series': 0.66; 'mar': 0.68; 'containing': 0.69; '2015': 0.84; 'end.': 0.84; 'treating': 0.84; 'utmost': 0.84; 'subject:looking': 0.91; 'to:none': 0.92; 'fun!': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=xQF4okmmG4WSyxJ3e2hOfTDTJTp43oqvBibfU908iH8=; b=lVzQ0CHVIH2tUdufWSj3bxsOOvJaf5lqBU8mctm/c3B8aCMd8MkgVtDRnZAkjaBvSD m2i58r+ux1t1ki2eOPheNiA+2JMekpYfNLVcfOV3fVtUm9JZ0bIHPsmfU/jDIuJ7m8zu R8pE7Krw5naOM7kAa5Ro7HG7DThk15pV+t233eyl148JJbxgGAo6YCAECcgra/7zIGbi 3Lueuic23McAelqUeCwogvMy69fZ9/40DlJsa0AIVk2JadY/T8RDgbMe47xsHZBKKCLV SV1IEcN73nk+La/7TaKXhEC7Q4GSg4cMX8y7rtTRg7lqjA96V7x1LPKkz7VfBTIVwmQn +04w== MIME-Version: 1.0 X-Received: by 10.107.160.212 with SMTP id j203mr11259164ioe.43.1427254093805; Tue, 24 Mar 2015 20:28:13 -0700 (PDT) In-Reply-To: References: Date: Wed, 25 Mar 2015 14:28:13 +1100 Subject: Re: Newbie looking for elegant solution From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427254102 news.xs4all.nl 2916 [2001:888:2000:d::a6]:60799 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87923 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. > > 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). > > Speed is not of utmost importance - an elegant solution is. Any suggestions? Oooh fun! >>> l = [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] 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. Example works in Python 3. For Python 2, you'll need ord() to get the integers at the end. I'm not sure how elegant this is, but it's a fun trick to play with :) Next idea please! I love these kinds of threads. ChrisA