Newbie looking for elegant solution

kai.peters at gmail.com kai.peters at gmail.com
Wed Mar 25 19:14:50 EDT 2015


On Tuesday, 24 March 2015 20:14:06 UTC-7, otaksoft... at 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).
> 
> The end result should be a new list that is 8 x shorter than the original list containing integers between 0 and 255.
> 
> Speed is not of utmost importance - an elegant solution is. Any suggestions?
> 
> Thanks for all input,
> Kai



The list comes from PILLOW:

getdata #

im.getdata() => sequence

Returns the contents of an image as a sequence object containing pixel values. The sequence object is flattened, so that values for line one follow directly after the values of line zero, and so on.

Note that the sequence object returned by this method is an internal PIL data type, which only supports certain sequence operations, including iteration and basic sequence access. To convert it to an ordinary sequence (e.g. for printing), use list(im.getdata()).




More information about the Python-list mailing list