python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
作者:踏破凌霄城 时间:2023-09-10 17:20:38
在抓取网络数据的时候,有时会用正则对结构化的数据进行提取,比如 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()函数的使用网站的支持!
来源:https://www.cnblogs.com/zrmw/p/10637114.html
标签:python,tuple,join,函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Centos7下源码安装Python3 及shell 脚本自动安装Python3的教程
2023-01-11 13:45:04
FLASH 全屏播放
2008-07-19 11:36:00
python写文件时覆盖原来的实例方法
2021-06-01 05:21:51
![](https://img.aspxhome.com/file/2023/3/123683_0s.jpg)
最新Python idle下载、安装与使用教程图文详解
2022-08-27 12:33:15
![](https://img.aspxhome.com/file/2023/9/125149_0s.png)
PyCharm搭建一劳永逸的开发环境
2022-12-23 20:24:23
![](https://img.aspxhome.com/file/2023/7/107657_0s.jpg)
ASP错误大全
2009-05-26 15:45:00
python实现凯撒密码加密解密的示例代码
2021-12-08 07:29:07
Python实现视频中添加音频工具详解
2022-06-03 12:32:28
SQL update 多表关联更新的实现代码
2024-01-22 01:44:06
js换图片效果可进行定时操作
2023-08-23 07:45:34
python设置值及NaN值处理方法
2022-11-30 01:48:42
python 实现二维列表转置
2021-04-20 10:00:06
Filestream使用简单步骤总结
2024-01-24 08:08:26
![](https://img.aspxhome.com/file/2023/5/113585_0s.png)
Keras预训练的ImageNet模型实现分类操作
2021-12-14 01:33:11
![](https://img.aspxhome.com/file/2023/3/123813_0s.jpg)
php实现网站留言板功能
2023-11-23 21:06:36
![](https://img.aspxhome.com/file/2023/1/111361_0s.jpg)
Go实现各类限流的方法
2024-04-27 15:39:47
![](https://img.aspxhome.com/file/2023/1/133961_0s.jpg)
vue中如何实现变量和字符串拼接
2024-04-30 10:21:22
![](https://img.aspxhome.com/file/2023/1/130261_0s.png)
Python抽象类的新写法
2022-12-04 13:39:38
Python使用定时调度任务的方式
2021-03-10 01:40:24
![](https://img.aspxhome.com/file/2023/9/76939_0s.jpg)
Go编写定时器与定时任务详解(附第三方库gocron用法)
2024-05-09 09:40:19