Pythonic list to bitflag mapping

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramon Felciano

    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
  • Steven Bethard

    #2
    Re: Pythonic list to bitflag mapping

    Ramon Felciano wrote:[color=blue]
    > 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[/color]
    [snip][color=blue]
    > [1 if x in a else 0 for x in range(10)][/color]

    How about:
    [color=blue][color=green][color=darkred]
    >>> a = [3,4,6]
    >>> [int(x in a) for x in range(10)][/color][/color][/color]
    [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

    Comment

    • Terry Hancock

      #3
      Re: Pythonic list to bitflag mapping

      On Friday 19 November 2004 06:47 pm, Ramon Felciano wrote:[color=blue]
      > a = [3,4,6]
      > b = [0,0,0,1,1,0,1,0 ,0,0] # a 1 for each position in the a list[/color]
      [...][color=blue]
      > 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)][/color]

      [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 anansispacework s.com )
      Anansi Spaceworks http://www.anansispaceworks.com

      Comment

      • Bengt Richter

        #4
        Re: Pythonic list to bitflag mapping

        On Fri, 19 Nov 2004 21:05:04 -0600, Terry Hancock <hancock@anansi spaceworks.com> wrote:
        [color=blue]
        >On Friday 19 November 2004 06:47 pm, Ramon Felciano wrote:[color=green]
        >> a = [3,4,6]
        >> b = [0,0,0,1,1,0,1,0 ,0,0] # a 1 for each position in the a list[/color]
        >[...][color=green]
        >> 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)][/color]
        >
        >[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)]
        >[/color]
        Or, since
        [color=blue][color=green][color=darkred]
        >>> issubclass(bool , int)[/color][/color][/color]
        True

        then
        [color=blue][color=green][color=darkred]
        >>> a = [3 ,4 ,6]
        >>> [x in a for x in xrange(10)][/color][/color][/color]
        [False, False, False, True, True, False, True, False, False, False]

        Is easily converted:[color=blue][color=green][color=darkred]
        >>> [int(x in a) for x in xrange(10)][/color][/color][/color]
        [0, 0, 0, 1, 1, 0, 1, 0, 0, 0]

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

        for whatever...[color=blue][color=green][color=darkred]
        >>> ''.join(['01'[x in a] for x in xrange(10)])[/color][/color][/color]
        '0001101000'

        etc.

        Regards,
        Bengt Richter

        Comment

        • Ramon Felciano

          #5
          Re: Pythonic list to bitflag mapping

          > Or can be used directly as an integer index to get a character[color=blue]
          >[color=green][color=darkred]
          > >>> ['01'[x in a] for x in xrange(10)][/color][/color]
          > ['0', '0', '0', '1', '1', '0', '1', '0', '0', '0']
          >[/color]
          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

          Comment

          • Ramon Felciano

            #6
            Re: Pythonic list to bitflag mapping

            > Or can be used directly as an integer index to get a character[color=blue]
            >[color=green][color=darkred]
            > >>> ['01'[x in a] for x in xrange(10)][/color][/color]
            > ['0', '0', '0', '1', '1', '0', '1', '0', '0', '0']
            >[/color]
            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

            Comment

            Working...