- 将
_list = ["h","e","l","l","o"]
转换成字符输出:
_list = ["h","e","l","l","o"]
print ",".join(a_list)
如果list中不是字符串,而是数字,则不能使用如上的方法,会有如下的错误:
TypeError: sequence item 0: expected string, int found
可以有以下的两种方法:
- 方法1
num_list = [0,1,2,3,4,5,6,7,8,9]
num_list_new = [str(x) for x in num_list]
print ",".join(num_list_new)
- 方法2
num_list = [0,1,2,3,4,5,6,7,8,9]
num_list_new = map(lambda x:str(x), num_list)
print ",".join(num_list_new)