map in Python

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

    #1

    map in Python

    I have recently switched over to Python from Perl. I want to do
    something like this in Python:

    @test = ("a1", "a2", "a3");
    map {s/[a-z]//g} @test;
    print @test;

    However, I take it there is no equivalent to $_ in Python. But in that
    case how does map pass the elements of a sequence to a function? I
    tried the following, but it doesn't work because the interpreter
    complains about a missing third argument to re.sub.

    import re
    test = ["a1", "a2", "a3"]
    map(re.sub("[a-z]", ""), test)
    print test
    Thanks in advance.

    Regards,
    Stuart <stuart AT zapata DOT org>

  • Simon Brunning

    #2
    Re: map in Python

    On 21 Jan 2005 04:25:27 -0800, Stu <stuart@zapata. org> wrote:[color=blue]
    > I have recently switched over to Python from Perl. I want to do
    > something like this in Python:
    >
    > @test = ("a1", "a2", "a3");
    > map {s/[a-z]//g} @test;
    > print @test;
    >
    > However, I take it there is no equivalent to $_ in Python. But in that
    > case how does map pass the elements of a sequence to a function? I
    > tried the following, but it doesn't work because the interpreter
    > complains about a missing third argument to re.sub.
    >
    > import re
    > test = ["a1", "a2", "a3"]
    > map(re.sub("[a-z]", ""), test)
    > print test[/color]

    This what you want?
    [color=blue][color=green][color=darkred]
    >>> import re
    >>> test = ["a1", "a2", "a3"]
    >>> test = [re.sub("[a-z]", "", item) for item in test]
    >>> test[/color][/color][/color]
    ['1', '2', '3']

    --
    Cheers,
    Simon B,
    simon@brunningo nline.net,

    Comment

    • Simon Brunning

      #3
      Re: map in Python

      On Fri, 21 Jan 2005 12:37:46 +0000, Simon Brunning
      <simon.brunning @gmail.com> wrote:[color=blue]
      > This what you want?
      >[color=green][color=darkred]
      > >>> import re
      > >>> test = ["a1", "a2", "a3"]
      > >>> test = [re.sub("[a-z]", "", item) for item in test]
      > >>> test[/color][/color]
      > ['1', '2', '3'][/color]

      Or, if you *must* use map, you can do:
      [color=blue][color=green][color=darkred]
      >>> test = map(lambda item: re.sub("[a-z]", "", item), test)
      >>> test[/color][/color][/color]
      ['1', '2', '3']

      I much prefer the first list comprehension form myself, but reasonable
      men can differ...

      --
      Cheers,
      Simon B,
      simon@brunningo nline.net,

      Comment

      • Pierre Barbier de Reuille

        #4
        Re: map in Python

        You have three ways to do what you want :

        First wayt is to use lambda. Then, you want to write :
        [color=blue][color=green][color=darkred]
        >>> map(lambda x: re.sub("[a-z]", "", x), test)[/color][/color][/color]

        Second is to use regular named function :
        [color=blue][color=green][color=darkred]
        >>> def remove_letters( s ):[/color][/color][/color]
        .... re.sub("[a-z]", "", s)[color=blue][color=green][color=darkred]
        >>> map(remove_lett ers, test)[/color][/color][/color]

        A third way would be to use the "pseudo-currying" described there :
        The official home of the Python Programming Language


        In fact, you need a small generalisation :
        [color=blue][color=green][color=darkred]
        >>> class curried(object) :[/color][/color][/color]
        .... def __init__(self, func, *a, **kw):
        .... self.func = func
        .... self.args = a
        .... self.kwords = kw
        .... def __call__(self, *a, **kw):
        .... args = self.args + a
        .... kwords = dict(self.kword s)
        .... kwords.update(k w)
        .... if len(args)+len(k words) < self.func.func_ code.co_argcoun t:
        .... return curried(self.fu nc, *args, **kwords)
        .... else:
        .... return self.func(*args , **kwords)

        The difference is you can handle the kwords with that version !
        Then you want to write this :
        [color=blue][color=green][color=darkred]
        >>> curried_sub = curried(re.sub)
        >>> map(curried_sub ("[a-z]", "", count=0), test)[/color][/color][/color]

        My opinion is : the simplest and best solution more "pythonic" is the
        second one ! The third one is interesting but work only for functions
        written in Python ! (Hopefully the re.sub function is written in
        Python). The biggest problem with the first one is that lambda are
        planned to disappear in future Python versions ...

        Pierre

        Stu a écrit :[color=blue]
        > I have recently switched over to Python from Perl. I want to do
        > something like this in Python:
        >
        > @test = ("a1", "a2", "a3");
        > map {s/[a-z]//g} @test;
        > print @test;
        >
        > However, I take it there is no equivalent to $_ in Python. But in that
        > case how does map pass the elements of a sequence to a function? I
        > tried the following, but it doesn't work because the interpreter
        > complains about a missing third argument to re.sub.
        >
        > import re
        > test = ["a1", "a2", "a3"]
        > map(re.sub("[a-z]", ""), test)
        > print test
        > Thanks in advance.
        >
        > Regards,
        > Stuart <stuart AT zapata DOT org>
        >[/color]

        Comment

        Working...