mapping None values to ''

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • micklee74@hotmail.com

    #1

    mapping None values to ''

    hi
    i wish to map None or "None" values to "".
    eg
    a = None
    b = None
    c = "None"

    map( <something> , [i for i in [a,b,c] if i in ("None",None ) ])

    I can't seem to find a way to put all values to "". Can anyone help?
    thanks

  • Tim Chase

    #2
    Re: mapping None values to ''

    > i wish to map None or "None" values to "".[color=blue]
    > eg
    > a = None
    > b = None
    > c = "None"
    >
    > map( <something> , [i for i in [a,b,c] if i in ("None",None ) ])
    >
    > I can't seem to find a way to put all values to "". Can anyone help?
    > thanks[/color]

    I'd consider this a VeryBadIdea(tm) . However, given Python's
    introspective superpowers, it *can* be done (even if it most
    likely *shouldn't* be done). That caveat out of the way, here goes:
    [color=blue][color=green][color=darkred]
    >>> for scope in [locals, globals]:[/color][/color][/color]
    .... s = scope()
    .... for vbl, value in s.items():
    .... if value == None or (type(value) == type("") and
    value == 'None'):
    .... s[vbl] = ''


    seems to do the trick. You may, likely, just want to operate on
    locals, not every last global variable in your project, in which
    case you can just use

    s = locals()
    for vbl, value in s.items():
    ...

    There may be some better way to determing whether an item is a
    string than my off-the-cufff

    type(value) == type("")

    but it worked to prevent trying to compare non-strings to the
    string 'None' later in the sub-clause of the if statement.

    Note that if you wrap this in a function, you'll may have to pass
    in locals() as a parameter because otherwise, inside your
    function, you'll have a different set of available locals that
    you did when you called the function. :)

    Beware your foot when shooting this gun...it likes to aim at feet.

    -tkc




    Comment

    • imho

      #3
      Re: mapping None values to ''

      micklee74@hotma il.com ha scritto:[color=blue]
      > hi
      > i wish to map None or "None" values to "".
      > eg
      > a = None
      > b = None
      > c = "None"
      >
      > map( <something> , [i for i in [a,b,c] if i in ("None",None ) ])
      >
      > I can't seem to find a way to put all values to "". Can anyone help?
      > thanks
      >[/color]

      You already filtered [a,b,c] in the comprehension list, so you just have
      to map all its values to "":

      map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None ) ])

      Comment

      • Gerard Flanagan

        #4
        Re: mapping None values to ''


        micklee74@hotma il.com wrote:[color=blue]
        > hi
        > i wish to map None or "None" values to "".
        > eg
        > a = None
        > b = None
        > c = "None"
        >
        > map( <something> , [i for i in [a,b,c] if i in ("None",None ) ])
        >
        > I can't seem to find a way to put all values to "". Can anyone help?
        > thanks[/color]

        a = [None, 'None', None]

        def filtre(x):
        if x is None or x == 'None':
        return ''
        else:
        return x

        assert map(filtre, a) == ['', '', '']

        a = [1, 2, None, 'None', 3, 4]

        assert map(filtre, a) == [1, 2, '', '', 3, 4]

        Comment

        • Roberto Bonvallet

          #5
          Re: mapping None values to ''

          imho <certo@comeno.i t>:[color=blue]
          > map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None ) ])[/color]

          You don't need map when using list comprehensions:

          ["" for i in [a, b, c] if i in ("None", None)]

          --
          Roberto Bonvallet

          Comment

          • Scott David Daniels

            #6
            Re: mapping None values to ''

            Roberto Bonvallet wrote:[color=blue]
            > imho <certo@comeno.i t>:[color=green]
            >> map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None ) ])[/color]
            > You don't need map when using list comprehensions:
            > ["" for i in [a, b, c] if i in ("None", None)]
            >[/color]
            More like:

            [(i, "")[i in ("None", None)] for i in [a,b,c]]

            --
            --Scott David Daniels
            scott.daniels@a cm.org

            Comment

            • Max Erickson

              #7
              Re: mapping None values to ''

              "Roberto Bonvallet" <rbonvall@gmail .com> wrote:[color=blue]
              > You don't need map when using list comprehensions:
              >
              > ["" for i in [a, b, c] if i in ("None", None)]
              >[/color]

              That loses list elements that aren't in the tests:
              [color=blue][color=green][color=darkred]
              >>> a=7
              >>> b="None"
              >>> c=None
              >>> ["" for i in [a,b,c] if i in ("None",None )][/color][/color][/color]
              ['', ''][color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              max

              Comment

              • Steven Bethard

                #8
                Re: mapping None values to ''

                Scott David Daniels wrote:[color=blue]
                > Roberto Bonvallet wrote:[color=green]
                >> imho <certo@comeno.i t>:[color=darkred]
                >>> map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None ) ])[/color]
                >> You don't need map when using list comprehensions:
                >> ["" for i in [a, b, c] if i in ("None", None)]
                >>[/color]
                > More like:
                >
                > [(i, "")[i in ("None", None)] for i in [a,b,c]][/color]

                Or in Python 2.5:

                Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit
                (Intel)] on win32
                Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
                >>> a = 0
                >>> b = None
                >>> c = None
                >>> a, b, c = ('' if x in (None, 'None') else x for x in (a, b, c))
                >>> a, b, c[/color][/color][/color]
                (0, '', '')

                But someone please shoot me if I ever use something like that in
                production code. ;)

                STeVe

                Comment

                • imho

                  #9
                  Re: mapping None values to ''

                  Roberto Bonvallet ha scritto:[color=blue]
                  > imho <certo@comeno.i t>:[color=green]
                  >> map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None ) ])[/color]
                  >
                  > You don't need map when using list comprehensions:
                  >
                  > ["" for i in [a, b, c] if i in ("None", None)]
                  >[/color]

                  I know that... I tried to match the idiom used by the o.p. :-)

                  Comment

                  Working...