473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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("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
Jul 18 '05 #1
6 1282
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
Jul 18 '05 #2
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
Jul 18 '05 #3
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
Jul 18 '05 #4
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
Jul 18 '05 #5
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
Jul 18 '05 #6
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>

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Zenon | last post by:
Folks, I am having a terrible time overloading operators. I have tried what I thought was the correct way, I tried the cheating (friend declarations), all to no avail. Sorry for posting tons of...
2
by: Michael Glaesemann | last post by:
Just finding out about the tinterval type (thanks again, Alvaro!), I've been looking to see what operators and functions are already built-in to PostgreSQL that involve this type. I thought I'd...
3
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my...
11
by: Jim Michaels | last post by:
friend fraction& operator+=(const fraction& rhs); fraction.h(64) Error: error: 'fraction& operator+=(const fraction&)' must take exactly two arguments I practically pulled this out of a C++...
39
by: vlsidesign | last post by:
Operators seem similar to functions. They both do something to either arguments or operands, but are different in their syntax. Operators seem to be like built-in C functions. It would seem that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.