python中join在2.X版本中的使用

Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串

例如:L = [1,2,3]   想让其显示为: 1,2,3

但是在2.X版本中,使用join()作用于L时,L中含有int类型的时候会报错

>>> L=[1,2,3]
>>> print ','.join(L)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found

 想要用2.X实现最终的结果,可以使用以下几种方法:

1、>>> print ','.join(map(str,L))
       1,2,3
2、>>> print ','.join(str(i) for i in L)
       1,2,3
3、>>> print ','.unicode(str(i) for i in L) 1,2,3

使用Unicode是为了防止有些字符串是unicode类型而产生的错误

posted @ 2016-01-07 16:07  勤&惰  阅读(258)  评论(0编辑  收藏  举报