码迷,mamicode.com
首页 > 其他好文 > 详细

字符串列表相互转换

时间:2018-12-11 16:05:17      阅读:1146      评论:0      收藏:0      [点我收藏+]

标签:expected   列表   com   相互   typeerror   spl   输出   join   cte   

1.字符串转列表   ----- split()

str1 = "hi hello world"
print(str1.split(" "))
输出:
[‘hi‘, ‘hello‘, ‘world‘]

 

2.列表转字符串  ----- " ".join()

l = ["hi","hello","world"]

#str = " ".join(l)     TypeError: sequence item 0: expected str instance, float found 

# ?? list包含数字,不能直接转化成字符串

# !! 解决办法对list中的元素进行类型转换到string 

str = " ".join(list(map(str, l)))

print(str)

输出:
hi hello world

 

参考转自:http://www.cnblogs.com/wills666/p/7007740.html

字符串列表相互转换

标签:expected   列表   com   相互   typeerror   spl   输出   join   cte   

原文地址:https://www.cnblogs.com/yasmine-93yang/p/10102196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!