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.

相关文章:

  • 2021-09-24
  • 2021-10-29
  • 2021-09-24
  • 2022-02-03
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-27
  • 2021-12-18
  • 2021-05-23
  • 2021-10-13
  • 2021-07-24
  • 2021-08-05
相关资源
相似解决方案