Python map

This is something I end up having to google for every now and then.

I have a list of numbers:

tmp = [1,2,3,4,5]

that I want to, in PHP talk is implode(), in python you can use the string.join() method like:

','.join( tmp )

But it says: TypeError: sequence item 0: expected string, int found, because it is a list of ints. There is a nice function called map() that as parameters takes a function and a list, then applies that function to each member of the list and returns a new list. We'll choose str() cos it casts an int to string:

','.join( map( str, tmp ) )

'1,2,3,4,5'


Nice.

posted @ 2012-04-05 15:48  yarpee  阅读(425)  评论(0编辑  收藏  举报