Operators as functions

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

    #1

    Operators as functions

    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("concati nating function", theList, "")

    And here is the questions: What to replace "concatinat ing 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 listcomprehensi on etc. I hope it
    is possible in Python too. In Haskell I would write:

    foldr (++) []

    Thank you for answering!

    --
    Anders Andersson
  • Steve Holden

    #2
    Re: Operators as functions

    Anders Andersson wrote:
    [color=blue]
    > 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("concati nating function", theList, "")
    >
    > And here is the questions: What to replace "concatinat ing 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 listcomprehensi on etc. I hope it
    > is possible in Python too. In Haskell I would write:
    >
    > foldr (++) []
    >
    > Thank you for answering!
    >[/color]
    [color=blue][color=green][color=darkred]
    >>> l = ["abc", "def", "ghi", "jkl"]
    >>> "".join(l)[/color][/color][/color]
    'abcdefghijkl'[color=blue][color=green][color=darkred]
    >>> ", ".join(l)[/color][/color][/color]
    'abc, def, ghi, jkl'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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

    Comment

    • Peter Hansen

      #3
      Re: Operators as functions

      Anders Andersson wrote:[color=blue]
      > 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("concati nating function", theList, "")
      >
      > And here is the questions: What to replace "concatinat ing 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 listcomprehensi on etc. I hope it
      > is possible in Python too. In Haskell I would write:[/color]

      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):
      [color=blue][color=green][color=darkred]
      >>> theList = range(10)
      >>> import operator
      >>> reduce(operator .add, map(str, theList))[/color][/color][/color]
      '0123456789'[color=blue][color=green][color=darkred]
      >>> ''.join(str(x) for x in theList)[/color][/color][/color]
      '0123456789'

      -Peter

      Comment

      • Steven Bethard

        #4
        Re: Operators as functions

        Peter Hansen wrote:[color=blue]
        > 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):
        >[color=green][color=darkred]
        > >>> theList = range(10)
        > >>> import operator
        > >>> reduce(operator .add, map(str, theList))[/color][/color]
        > '0123456789'[color=green][color=darkred]
        > >>> ''.join(str(x) for x in theList)[/color][/color]
        > '0123456789'[/color]

        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(operato r.add, map(str, L))"
        10 loops, best of 3: 89.9 msec per loop

        $ python -m timeit -s "L = range(10000)" "''.join(st r(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

        Comment

        • Anders Andersson

          #5
          Re: Operators as functions

          Steve Holden wrote:[color=blue]
          > Anders Andersson wrote:
          >[color=green]
          >> 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("concati nating function", theList, "")
          >>
          >> And here is the questions: What to replace "concatinat ing 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 listcomprehensi on
          >> etc. I hope it is possible in Python too. In Haskell I would write:
          >>
          >> foldr (++) []
          >>
          >> Thank you for answering!
          >>[/color]
          >[color=green][color=darkred]
          > >>> l = ["abc", "def", "ghi", "jkl"]
          > >>> "".join(l)[/color][/color]
          > 'abcdefghijkl'[color=green][color=darkred]
          > >>> ", ".join(l)[/color][/color]
          > 'abc, def, ghi, jkl'[color=green][color=darkred]
          > >>>[/color][/color]
          >
          > regards
          > Steve[/color]

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

          --
          Anders Andersson

          Comment

          • Anders Andersson

            #6
            Re: Operators as functions

            Peter Hansen wrote:[color=blue]
            > Anders Andersson wrote:
            >[color=green]
            >> 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("concati nating function", theList, "")
            >>
            >> And here is the questions: What to replace "concatinat ing 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 listcomprehensi on
            >> etc. I hope it is possible in Python too. In Haskell I would write:[/color]
            >
            >
            > 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):
            >[color=green][color=darkred]
            > >>> theList = range(10)
            > >>> import operator
            > >>> reduce(operator .add, map(str, theList))[/color][/color]
            > '0123456789'[color=green][color=darkred]
            > >>> ''.join(str(x) for x in theList)[/color][/color]
            > '0123456789'
            >
            > -Peter[/color]

            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

            Comment

            • Fredrik Lundh

              #7
              Re: Operators as functions

              Anders Andersson wrote:
              [color=blue]
              > I want to concatinate (I apologize for bad English, but it is not my native language)[/color]

              fast det stavas iofs inte konkatinera på svenska heller ;-)
              [color=blue]
              > a list of strings to a string.[/color]
              [color=blue]
              > And here is the questions: What to replace "concatinat ing function" with? Can I in some way give
              > the +-operator as an argument to the reduce function?[/color]

              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>



              Comment

              Working...