Operators as functions

A

Anders Andersson

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!
 
S

Steve Holden

Anders said:
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
 
P

Peter Hansen

Anders said:
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):
'0123456789'

-Peter
 
S

Steven Bethard

Peter said:
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):

'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
 
A

Anders Andersson

Steve said:
Anders said:
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!
 
A

Anders Andersson

Peter said:
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):

'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!
 
F

Fredrik Lundh

Anders said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top