map in Python

Discussion in 'Python' started by Stu, Jan 21, 2005.

  1. Stu

    Stu Guest

    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>
     
    Stu, Jan 21, 2005
    #1
    1. Advertising

  2. On 21 Jan 2005 04:25:27 -0800, Stu <> wrote:
    > 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


    This what you want?

    >>> import re
    >>> test = ["a1", "a2", "a3"]
    >>> test = [re.sub("[a-z]", "", item) for item in test]
    >>> test

    ['1', '2', '3']

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Jan 21, 2005
    #2
    1. Advertising

  3. On Fri, 21 Jan 2005 12:37:46 +0000, Simon Brunning
    <> wrote:
    > This what you want?
    >
    > >>> import re
    > >>> test = ["a1", "a2", "a3"]
    > >>> test = [re.sub("[a-z]", "", item) for item in test]
    > >>> test

    > ['1', '2', '3']


    Or, if you *must* use map, you can do:

    >>> test = map(lambda item: re.sub("[a-z]", "", item), test)
    >>> test

    ['1', '2', '3']

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

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Jan 21, 2005
    #3
  4. You have three ways to do what you want :

    First wayt is to use lambda. Then, you want to write :

    >>> map(lambda x: re.sub("[a-z]", "", x), test)


    Second is to use regular named function :

    >>> def remove_letters( s ):

    .... re.sub("[a-z]", "", s)
    >>> map(remove_letters, test)


    A third way would be to use the "pseudo-currying" described there :
    http://www.python.org/moin/PythonDecoratorLibrary

    In fact, you need a small generalisation :

    >>> class curried(object):

    .... 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.kwords)
    .... kwords.update(kw)
    .... if len(args)+len(kwords) < self.func.func_code.co_argcount:
    .... return curried(self.func, *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 :

    >>> curried_sub = curried(re.sub)
    >>> map(curried_sub("[a-z]", "", count=0), test)


    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 :
    > 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>
    >
     
    Pierre Barbier de Reuille, Jan 21, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex
    Replies:
    1
    Views:
    654
    Lau Lei Cheong
    Feb 4, 2005
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    7,992
    krogers
    Mar 20, 2012
  3. Vlad
    Replies:
    0
    Views:
    366
  4. Patrick Guio
    Replies:
    6
    Views:
    3,214
    chris
    Oct 20, 2004
  5. Erik Arner
    Replies:
    0
    Views:
    1,316
    Erik Arner
    Nov 2, 2004
Loading...

Share This Page