how to join array of integers?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Sep 16 02:28:56 CEST 2007


On Sat, 15 Sep 2007 16:07:07 +0000, Grant Edwards wrote:


> It's nice people have invented so many ways to spell the builting "map"
> ;)
> 
>>>> ",".join(map(str,[1,2,3]))
> '1,2,3'


The oldest solution, and if not the fastest, at least neck-and-neck with 
the list comprehension.

>>> timeit.Timer("', '.join(map(str, [1,2,3]))", "").repeat()
[5.0320308208465576, 4.1513419151306152, 4.0970909595489502]


For those who missed my earlier post:

list comp, best of three for one million iterations: 4.53 seconds
generator expression: 10.52 seconds
itertools.imap: 8.52 seconds

Your mileage may vary.

I think it is a crying shame that Guido's prejudice against functional 
programming seems to have increased over the years, instead of decreased. 
Iterators are wonderful tools, but professional tradesmen use more than 
one sort of hammer. (There are claw hammers and ball peen hammers and 
tack hammers and wooden mallets and...)

"One obvious way to do it" should not mean "force everyone to use a tack 
hammer to drive in nails, because it's the only hammer in the tool box". 
It should mean "it's obvious, use the tack hammer to drive in tacks and 
the claw hammer for carpentry and the wooden mallet for beating out dints 
in sheet metal and..."


-- 
Steven.



More information about the Python-list mailing list