473,508 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pythonic list to bitflag mapping

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
Jul 18 '05 #1
5 1328
Ramon Felciano wrote:
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
Jul 18 '05 #2
On Friday 19 November 2004 06:47 pm, Ramon Felciano wrote:
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

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 18 '05 #3
On Fri, 19 Nov 2004 21:05:04 -0600, Terry Hancock <ha*****@anansispaceworks.com> wrote:
On Friday 19 November 2004 06:47 pm, Ramon Felciano wrote:
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
issubclass(bool, int) 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
Jul 18 '05 #4
> 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
Jul 18 '05 #5
> 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
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2253
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
10
5818
by: Bulba! | last post by:
Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary...
41
3496
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
2
1713
by: jmdeschamps | last post by:
Working with several thousand tagged items on a Tkinter Canvas, I want to change different configurations of objects having a certain group of tags. I've used the sets module, on the tuple...
4
1784
by: Carl J. Van Arsdall | last post by:
It seems the more I come to learn about Python as a langauge and the way its used I've come across several discussions where people discuss how to do things using an OO model and then how to design...
33
2252
by: Gregory Petrosyan | last post by:
Buenos dias, amigos! I have to write _simple_ gui library, for embedding into game. My first attempt was to use XML: isn't it cute to describe ui in such a way: <window> <title>Hello...
5
2198
by: akameswaran | last post by:
Disclaimer - I recognize this is not a practical exercise. There are many implementations around that would do the job better, more efficiently (Meaning in C) or whatever. I caught some thread...
0
3451
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement...
26
1822
by: Frank Samuelson | last post by:
I love Python, and it is one of my 2 favorite languages. I would suggest that Python steal some aspects of the S language. ------------------------------------------------------- 1. Currently...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.