snacktime wrote: > I need to convert a generator expression to a list expression so it > will work under python 2.3. > > I rewrote this: > > for c in range(128): > even_odd = (sum(bool(c & 1<<b) for b in range(8))) & 1 > > As this: > > for c in range(128): > bo = [bool(c & 1<<b) for b in range(8)] > even_odd = sum(bo) & 1 > > > Seems to work, is there a better way to do this?
Summing over zeros seems pointless, so >>> for c in range(128): ... print len([1 for b in range(8) if c & 1 << b]) & 1, ... 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 The same simplification works for genexps, but you have to use sum() there instead of len(). Another optimization would be to precalculate the bitmasks [1 << b for b in range(8)] outside the loop. Peter -- http://mail.python.org/mailman/listinfo/python-list