python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
2019-05-08 18:02
1096 查看
在抓取网络数据的时候,有时会用正则对结构化的数据进行提取,比如 href="https://www.1234.com"等。python的re模块的findall()函数会返回一个所有匹配到的内容的列表,在将数据存入数据库时,列表数据类型是不被允许的,而是需要将其转换为元组形式。下面看下,str/list/tuple三者之间怎么相互转换。
class forDatas: def __init__(self): pass def str_list_tuple(self): s = 'abcde12345' print('s:', s, type(s)) # str to list l = list(s) print('l:', l, type(l)) # str to tuple t = tuple(s) print('t:', t, type(t)) # str转化为list/tuple,直接进行转换即可 # 由list/tuple转换为str,则需要借助join()函数来实现 # list to str s1 = ''.join(l) print('s1:', s1, type(s1)) # tuple to str s2 = ''.join(t) print('s2:', s2, type(s2))
str转化为list/tuple,直接进行转换即可。而由list/tuple转换为str,则需要借助join()函数来实现。join()函数是这样描述的:
""" S.join(iterable) -> str Return a string which is the concatenation of the strings in the iterable. The separator between elements is S. """
join()函数使用时,传入一个可迭代对象,返回一个可迭代的字符串,该字符串元素之间的分隔符是“S”。
传入一个可迭代对象,可以使list,tuple,也可以是str。
s = 'asdf1234' sss = '@'.join(s) print(type(sss), sss)
总结
以上所述是小编给大家介绍的python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
相关文章推荐
- python 中list(列表),tupe(元组),str(字符串),dict(字典)之间的相互转换
- python中str(字符串)、list(列表)、tuple(元组)、dict(字典)相互转化关系及字典键-值遍历
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- 人生苦短之我用Python篇(列表list、字典dict、元组tuple、字符串str)
- python3 list、tuple(元组)、str之间的相互转换
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- python中列表,元组,字典,字符串相互转换
- python学习之路-字符串(str)和列表(list)的互相转换
- python中列表元组字符串相互转换
- Python 实现字符串转换成列表 实现str转换list
- Python中list、tuple、str和dict之间的相互转换
- Python字符串、元组、列表、字典互相转换的方法
- python 字符串(str)和列表(list)的互相转换
- 收集: python中列表,元组,字符串 相互转换, IO 数据处理,行数据切割
- Python数据结构 字符串,列表list,元组tuple,字典dict,容器
- String(字符串)、List(列表)、Tuple(元组)、Dictionary(字典)转换
- Python中list、tuple、str和dict之间的相互转换
- leetcode_345. Reverse Vowels of a String 转换字符串中的元音字符,list类型数据转换成str类型,join()函数
- Python中list、tuple、str和dict之间的相互转换
- Dive Into Python 学习记录1-函数/模块导入/字典/列表/元组/字符串分割、连接、格式化/映射list/