Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87964
| Subject | Re: Newbie looking for elegant solution |
|---|---|
| From | Travis Griggs <travisgriggs@gmail.com> |
| Date | 2015-03-25 11:49 -0700 |
| References | <bc226abc-2860-47d9-9d75-8e1ad1cae097@googlegroups.com> <CAPTjJmpZSod0GrPf-WLsQgMwbX5P2vV+iGDeYTyNsSqC1CHDuQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.153.1427309403.10327.python-list@python.org> (permalink) |
> On Mar 24, 2015, at 8:28 PM, Chris Angelico <rosuav@gmail.com> wrote:
>
> On Wed, Mar 25, 2015 at 2:13 PM, <otaksoftspamtrap@gmail.com> 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.
Me too. These are my favorite threads. Here’s 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Newbie looking for elegant solution otaksoftspamtrap@gmail.com - 2015-03-24 20:13 -0700
Re: Newbie looking for elegant solution Chris Angelico <rosuav@gmail.com> - 2015-03-25 14:28 +1100
Re: Newbie looking for elegant solution otaksoftspamtrap@gmail.com - 2015-03-24 20:31 -0700
Re: Newbie looking for elegant solution Paul Rubin <no.email@nospam.invalid> - 2015-03-24 21:04 -0700
Re: Newbie looking for elegant solution kai.peters@gmail.com - 2015-03-24 21:19 -0700
Re: Newbie looking for elegant solution Chris Angelico <rosuav@gmail.com> - 2015-03-25 15:19 +1100
Re: Newbie looking for elegant solution kai.peters@gmail.com - 2015-03-24 21:46 -0700
Re: Newbie looking for elegant solution Chris Angelico <rosuav@gmail.com> - 2015-03-25 16:05 +1100
Re: Newbie looking for elegant solution Ben Finney <ben+python@benfinney.id.au> - 2015-03-25 16:28 +1100
Re: Newbie looking for elegant solution Paul Rubin <no.email@nospam.invalid> - 2015-03-24 22:52 -0700
Re: Newbie looking for elegant solution Rustom Mody <rustompmody@gmail.com> - 2015-03-24 23:32 -0700
Re: Newbie looking for elegant solution Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-25 16:24 +1100
Re: Newbie looking for elegant solution Dave Farrance <DaveFarrance@OMiTTHiSyahooANDTHiS.co.uk> - 2015-03-25 06:05 +0000
Re: Newbie looking for elegant solution Travis Griggs <travisgriggs@gmail.com> - 2015-03-25 11:49 -0700
Re: Newbie looking for elegant solution kai.peters@gmail.com - 2015-03-25 16:14 -0700
Re: Newbie looking for elegant solution Paul Rubin <no.email@nospam.invalid> - 2015-03-25 16:30 -0700
Re: Newbie looking for elegant solution Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2015-03-26 00:34 +0100
Re: Newbie looking for elegant solution kai.peters@gmail.com - 2015-03-25 17:38 -0700
Re: Newbie looking for elegant solution Paul Rubin <no.email@nospam.invalid> - 2015-03-25 18:09 -0700
Re: Newbie looking for elegant solution kai.peters@gmail.com - 2015-03-25 18:29 -0700
Re: Newbie looking for elegant solution Paul Rubin <no.email@nospam.invalid> - 2015-03-25 19:53 -0700
csiph-web