Operators as functions

Discussion in 'Python' started by Anders Andersson, Dec 20, 2004.

  1. Hello

    I want to concatinate (I apologize for bad English, but it is not my
    native language) a list of strings to a string. I could use (I think):

    s = ""
    map(lambda x: s.append(x), theList)

    But I want to do something like (I think that the code above is clumsy):

    s = reduce("concatinating function", theList, "")

    And here is the questions: What to replace "concatinating function"
    with? Can I in some way give the +-operator as an argument to the reduce
    function? I know operators can be sent as arguments in Haskell and since
    Python has functions as map, filter and listcomprehension etc. I hope it
    is possible in Python too. In Haskell I would write:

    foldr (++) []

    Thank you for answering!

    --
    Anders Andersson
     
    Anders Andersson, Dec 20, 2004
    #1
    1. Advertising

  2. Anders Andersson

    Steve Holden Guest

    Anders Andersson wrote:

    > Hello
    >
    > I want to concatinate (I apologize for bad English, but it is not my
    > native language) a list of strings to a string. I could use (I think):
    >
    > s = ""
    > map(lambda x: s.append(x), theList)
    >
    > But I want to do something like (I think that the code above is clumsy):
    >
    > s = reduce("concatinating function", theList, "")
    >
    > And here is the questions: What to replace "concatinating function"
    > with? Can I in some way give the +-operator as an argument to the reduce
    > function? I know operators can be sent as arguments in Haskell and since
    > Python has functions as map, filter and listcomprehension etc. I hope it
    > is possible in Python too. In Haskell I would write:
    >
    > foldr (++) []
    >
    > Thank you for answering!
    >


    >>> l = ["abc", "def", "ghi", "jkl"]
    >>> "".join(l)

    'abcdefghijkl'
    >>> ", ".join(l)

    'abc, def, ghi, jkl'
    >>>


    regards
    Steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
     
    Steve Holden, Dec 20, 2004
    #2
    1. Advertising

  3. Anders Andersson

    Peter Hansen Guest

    Anders Andersson wrote:
    > I want to concatinate (I apologize for bad English, but it is not my
    > native language) a list of strings to a string. I could use (I think):
    >
    > s = ""
    > map(lambda x: s.append(x), theList)
    >
    > But I want to do something like (I think that the code above is clumsy):
    >
    > s = reduce("concatinating function", theList, "")
    >
    > And here is the questions: What to replace "concatinating function"
    > with? Can I in some way give the +-operator as an argument to the reduce
    > function? I know operators can be sent as arguments in Haskell and since
    > Python has functions as map, filter and listcomprehension etc. I hope it
    > is possible in Python too. In Haskell I would write:


    You are looking for "import operator", followed by a use of
    operator.add in the reduce() call. Note, however, that you
    cannot add different types (generally) together, so you will
    run into trouble if you take the "naive" approach and just
    trying concatenating the strings and the list as you show
    above. Instead, you will need to pass the sequence of
    things through a call to map(str, sequence) first, to call
    the str() method on everything and make sure you are adding
    strings together. Thus:

    import operator
    s = reduce(operator.add, map(str, theList))

    or something like that.

    However, none of this is considered the best approach these days,
    with the advent of list comprehensions and generator expressions.
    Here's the old approach (shown above) and the shiny new modern
    Pythonic approach (requires Python 2.4):

    >>> theList = range(10)
    >>> import operator
    >>> reduce(operator.add, map(str, theList))

    '0123456789'
    >>> ''.join(str(x) for x in theList)

    '0123456789'

    -Peter
     
    Peter Hansen, Dec 20, 2004
    #3
  4. Peter Hansen wrote:
    > However, none of this is considered the best approach these days,
    > with the advent of list comprehensions and generator expressions.
    > Here's the old approach (shown above) and the shiny new modern
    > Pythonic approach (requires Python 2.4):
    >
    > >>> theList = range(10)
    > >>> import operator
    > >>> reduce(operator.add, map(str, theList))

    > '0123456789'
    > >>> ''.join(str(x) for x in theList)

    > '0123456789'


    Also worth noting is that the shiny new modern version is also the
    faster version:

    $ python -m timeit -s "import operator; L = range(10000)"
    "reduce(operator.add, map(str, L))"
    10 loops, best of 3: 89.9 msec per loop

    $ python -m timeit -s "L = range(10000)" "''.join(str(x) for x in L)"
    100 loops, best of 3: 15.3 msec per loop

    [run with Python 2.4 on a 2.26GHz Pentium 4]

    Steve
     
    Steven Bethard, Dec 20, 2004
    #4
  5. Steve Holden wrote:
    > Anders Andersson wrote:
    >
    >> Hello
    >>
    >> I want to concatinate (I apologize for bad English, but it is not my
    >> native language) a list of strings to a string. I could use (I think):
    >>
    >> s = ""
    >> map(lambda x: s.append(x), theList)
    >>
    >> But I want to do something like (I think that the code above is clumsy):
    >>
    >> s = reduce("concatinating function", theList, "")
    >>
    >> And here is the questions: What to replace "concatinating function"
    >> with? Can I in some way give the +-operator as an argument to the
    >> reduce function? I know operators can be sent as arguments in Haskell
    >> and since Python has functions as map, filter and listcomprehension
    >> etc. I hope it is possible in Python too. In Haskell I would write:
    >>
    >> foldr (++) []
    >>
    >> Thank you for answering!
    >>

    >
    > >>> l = ["abc", "def", "ghi", "jkl"]
    > >>> "".join(l)

    > 'abcdefghijkl'
    > >>> ", ".join(l)

    > 'abc, def, ghi, jkl'
    > >>>

    >
    > regards
    > Steve


    Quiet unexpected, but very beautiful. I will use this! Thank you for
    replaying!

    --
    Anders Andersson
     
    Anders Andersson, Dec 21, 2004
    #5
  6. Peter Hansen wrote:
    > Anders Andersson wrote:
    >
    >> I want to concatinate (I apologize for bad English, but it is not my
    >> native language) a list of strings to a string. I could use (I think):
    >>
    >> s = ""
    >> map(lambda x: s.append(x), theList)
    >>
    >> But I want to do something like (I think that the code above is clumsy):
    >>
    >> s = reduce("concatinating function", theList, "")
    >>
    >> And here is the questions: What to replace "concatinating function"
    >> with? Can I in some way give the +-operator as an argument to the
    >> reduce function? I know operators can be sent as arguments in Haskell
    >> and since Python has functions as map, filter and listcomprehension
    >> etc. I hope it is possible in Python too. In Haskell I would write:

    >
    >
    > You are looking for "import operator", followed by a use of
    > operator.add in the reduce() call. Note, however, that you
    > cannot add different types (generally) together, so you will
    > run into trouble if you take the "naive" approach and just
    > trying concatenating the strings and the list as you show
    > above. Instead, you will need to pass the sequence of
    > things through a call to map(str, sequence) first, to call
    > the str() method on everything and make sure you are adding
    > strings together. Thus:
    >
    > import operator
    > s = reduce(operator.add, map(str, theList))
    >
    > or something like that.
    >
    > However, none of this is considered the best approach these days,
    > with the advent of list comprehensions and generator expressions.
    > Here's the old approach (shown above) and the shiny new modern
    > Pythonic approach (requires Python 2.4):
    >
    > >>> theList = range(10)
    > >>> import operator
    > >>> reduce(operator.add, map(str, theList))

    > '0123456789'
    > >>> ''.join(str(x) for x in theList)

    > '0123456789'
    >
    > -Peter


    Thank you for replaying. The operator.add is new to me and I will keep
    it in mind. It will perhaps come to use. I will use the join function
    since it looks more beatiful!

    --
    Anders Andersson
     
    Anders Andersson, Dec 21, 2004
    #6
  7. Anders Andersson wrote:

    > I want to concatinate (I apologize for bad English, but it is not my native language)


    fast det stavas iofs inte konkatinera på svenska heller ;-)

    > a list of strings to a string.


    > And here is the questions: What to replace "concatinating function" with? Can I in some way give
    > the +-operator as an argument to the reduce function?


    see the operator module.

    but as other have already pointed out, turning a list of strings into a single
    string is usually written as:

    s = "".join(seq)

    in contemporary Python, or

    import string
    s = string.join(seq, "")

    in pre-unicode python style (note that in the first case, the "join" operation is
    actually a method of the separator. seq can be any object that can produce
    a sequence. it looks a bit weird, though...)

    you can solve this by repeatedly adding individual strings, but that's rather
    costly: first, your code will copy string 1 and 2 to a new string (let's call it
    A). then your code will copy A and string 3 to a new string B. then your
    code will copy B and string 4 to a new string C. etc. lots of unnecessary
    copying.

    the join method, in contrast, does it all in one operation.

    </F>
     
    Fredrik Lundh, Dec 21, 2004
    #7
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,652
    Victor Bazarov
    Jun 21, 2004
  2. Michael Yanowitz

    Functions, Operators, and Overloading?

    Michael Yanowitz, Jul 24, 2006, in forum: Python
    Replies:
    6
    Views:
    424
    Bruno Desthuilliers
    Jul 24, 2006
  3. JohnQ
    Replies:
    6
    Views:
    374
    JohnQ
    Feb 14, 2007
  4. vlsidesign

    operators similar to functions?

    vlsidesign, Nov 29, 2007, in forum: C Programming
    Replies:
    72
    Views:
    1,554
    David Thompson
    Dec 24, 2007
  5. Stephen Howe
    Replies:
    3
    Views:
    258
    Öö Tiib
    Dec 2, 2012
Loading...

Share This Page