python字符串与列表的相互转换
2019-05-05 22:44
246 查看
python 把列表转化为字符串的方法
>>> list1=['ak','uk',4] >>> list2=[str(i) for i in list1] #使用列表推导式把列表中的单个元素全部转化为str类型 >>> list2 #查看转化后的列表 ['ak', 'uk', '4'] >>> list3=' '.join(list2) #把列表中的元素放在空串中,元素间用空格隔开 >>> list3 #查看生成的长串 'ak uk 4'
用print方法直接直接打印出串中的元素,不显示格式
>>> a=["1","2","3","4","5"] #列表中的元素全是str类型 >>> print(" ".join(a)) #把列表中的元素放在空串了,并打印出空串中的内容 1 2 3 4 5 >>> b=[1,2,3,4,5] >>> c=map(str,b) #列表中的元素并不是str类型,需要把b中的元素map成str类型 >>> type(b) <class 'list'> >>> type(c) <class 'map'> >>> print(" ".join(c)) #这里为什么用c而不是b我也没有搞清楚?? 1 2 3 4 5
1. 字符串转列表 str1 = "hi hello world" print(str1.split(" ")) 输出: ['hi', 'hello', 'world'] 2. 列表转字符串 l = ["hi","hello","world"] print(" ".join(l)) 输出: hi hello world
转自:https://www.geek-share.com/detail/2751258528.html
https://www.cnblogs.com/wills666/p/7007740.html#undefined
相关文章推荐
- 收集: python中列表,元组,字符串 相互转换, IO 数据处理,行数据切割
- python中列表,元组,字典,字符串相互转换
- python中列表元组字符串相互转换
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python 中list(列表),tupe(元组),str(字符串),dict(字典)之间的相互转换
- python中字符串和列表的相互转换
- python列表,元组,字典和字符串之间的相互转换
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python 字符串,列表,元组,字典相互转换
- python字符串列表字典相互转换
- python 字符串,列表,元组,字典相互转换
- python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
- python字符串、列表、字典相互转换
- python字符串与列表的相互转换
- Python字符串、元组、列表、字典互相转换的方法
- python--列表,元组,字符串互相转换
- python中 将字符串和字典的相互转换
- python中字符串与datetime的相互转换
- python 列表转换成字符串输出
- Python 实现字符串转换成列表 实现str转换list