How to Read Bytes from a file
Alex Martelli
aleax at mac.com
Thu Mar 1 10:55:12 EST 2007
Leif K-Brooks <eurleif at ecritters.biz> wrote:
> Alex Martelli wrote:
> > You should probaby prepare before the loop a mapping from char to number
> > of 1 bits in that char:
> >
> > m = {}
> > for c in range(256):
> > m[c] = countones(c)
>
> Wouldn't a list be more efficient?
>
> m = [countones(c) for c in xrange(256)]
Yes, or an array.array -- actually I meant to use m[chr(c)] above (so
you could use the character you're reading directly to index m, rather
than calling ord(byte) a bazillion times for each byte you're reading),
but if you're using the numbers (as I did before) a list or array is
better.
Alex
More information about the Python-list
mailing list