Pythonic list to bitflag mapping

Ramon Felciano felciano at ingenuity.com
Fri Nov 19 19:47:03 EST 2004


Hi --

I have a list of integers that I'd like to convert to a bitflag or 1D
matrix format:

a = [3,4,6]
b = [0,0,0,1,1,0,1,0,0,0] # a 1 for each position in the a list

The specifics of the resulting representation is irrelevant; it could
be ' ' and 'X' characters. Eventually this will be printed in a grid
format, so the key is converting those numbers to an appropriate
positional flag in the result list.

I can do this with loops etc, but was curious if there isn't a python
one-liner or list comprehension way of doing it. What I'd like to
write is something like the following:

[1 if x in a else 0 for x in range(10)]

Any tips?

Thanks!

Ramon



More information about the Python-list mailing list