Pythonic list to bitflag mapping

R

Ramon Felciano

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
 
S

Steven Bethard

Ramon said:
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 [snip]
[1 if x in a else 0 for x in range(10)]

How about:
>>> a = [3,4,6]
>>> [int(x in a) for x in range(10)]
[0, 0, 0, 1, 1, 0, 1, 0, 0, 0]

The idea here is to treat the bool 'x in a' as an int, making use of the
fact that int(True) == 1 and int(False) == 0.

Steve
 
T

Terry Hancock

a = [3,4,6]
b = [0,0,0,1,1,0,1,0,0,0] # a 1 for each position in the a 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)]

[x in a for x in range(10)]

Though this will return boolean True/False values in Python 2.3+ and
an int in earlier versions. However conditional operations will work
on either, so your code may tolerate this.

If you must have 0 and 1 though, you can use:

[(x in a and 1 or 0) for x in range(10)]

Cheers,
Terry
 
B

Bengt Richter

a = [3,4,6]
b = [0,0,0,1,1,0,1,0,0,0] # a 1 for each position in the a 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)]

[x in a for x in range(10)]

Though this will return boolean True/False values in Python 2.3+ and
an int in earlier versions. However conditional operations will work
on either, so your code may tolerate this.

If you must have 0 and 1 though, you can use:

[(x in a and 1 or 0) for x in range(10)]
Or, since
True

then
>>> a = [3 ,4 ,6]
>>> [x in a for x in xrange(10)]
[False, False, False, True, True, False, True, False, False, False]

Is easily converted:
>>> [int(x in a) for x in xrange(10)]
[0, 0, 0, 1, 1, 0, 1, 0, 0, 0]

Or can be used directly as an integer index to get a character
>>> ['01'[x in a] for x in xrange(10)]
['0', '0', '0', '1', '1', '0', '1', '0', '0', '0']

for whatever...
>>> ''.join(['01'[x in a] for x in xrange(10)])
'0001101000'

etc.

Regards,
Bengt Richter
 
R

Ramon Felciano

Or can be used directly as an integer index to get a character
['01'[x in a] for x in xrange(10)]
['0', '0', '0', '1', '1', '0', '1', '0', '0', '0']
Very cool -- this does the trick nicely and seems quite extensible,
now that I get the basic idiom.

Belated thanks for the quick replies on this one!

Ramon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top