Python – list与字符串str相互转换方法总结
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Python – list与字符串str相互转换方法总结
原文链接:https://www.stubbornhuang.com/2188/
发布于:2022年06月30日 9:06:24
修改于:2022年06月30日 9:06:24

1 字符串str转list的方法总结
# -*- coding: utf-8 -*-
if __name__ == '__main__':
temp_str = '1once time is wasted2';
temp_list = list(temp_str)
print(temp_list)
输出
['1', 'o', 'n', 'c', 'e', ' ', 't', 'i', 'm', 'e', ' ', 'i', 's', ' ', 'w', 'a', 's', 't', 'e', 'd', '2']
2 list转字符串str方法总结
2.1 join方法
(1) 当list中的元素全部为str类型时
# -*- coding: utf-8 -*-
if __name__ == '__main__':
example_list = ['once','time','is','wasted']
example_list_str = (" ").join(example_list)
print(example_list_str)
(2) 当list中的元素为str和数字类型混合时
# -*- coding: utf-8 -*-
if __name__ == '__main__':
example_list = ['1','once','time','is','wasted','2']
example_list_str = (" ").join("%s"%i for i in example_list)
print(example_list_str)
或者
# -*- coding: utf-8 -*-
if __name__ == '__main__':
example_list = ['1','once','time','is','wasted','2']
example_list_copy = list(map(lambda x: str(x), example_list))
example_list_str = (" ").join(example_list_copy)
print(example_list_str)
或者
# -*- coding: utf-8 -*-
if __name__ == '__main__':
example_list = ['1','once','time','is','wasted','2']
example_list_str = (" ").join(str(i) for i in example_list)
print(example_list_str)
当前分类随机文章推荐
- Python - 读取csv文件和在csv文件写入内容 阅读379次,点赞0次
- Python – 解决opencv-python使用cv2.imwrite()保存中文路径图片失败的问题 阅读1303次,点赞0次
- Pip - 常用命令(安装,卸载,升级第三方库) 阅读3128次,点赞1次
- Python - 使用Python+websockets时报错:AttributeError: module 'websockets' has no attribute 'serve' 阅读1500次,点赞0次
- Python - 使用代码判断当前Python版本号 阅读277次,点赞0次
- Python - 运算符/ or // or %的含义和区别 阅读1801次,点赞0次
- Python - 使用命令行调用ffmpeg修改视频帧率,将60FPS的视频修改为30FPS的视频,视频时间保持不变 阅读299次,点赞0次
- opencv-python - 读取视频,不改变视频分辨率修改视频帧率 阅读4682次,点赞2次
- Python3爬虫 - 下载反盗链图片的方式 阅读2710次,点赞1次
- Python3爬虫 - requests库的requests.exceptions所有异常详细说明 阅读5397次,点赞2次
全站随机文章推荐
- 深度学习 - 我的深度学习项目代码文件组织结构 阅读1057次,点赞3次
- 资源分享 - Computer Graphics with OpenGL , Third Edition 英文高清PDF下载 阅读966次,点赞0次
- 资源分享 - GPU Pro 360 - Guide to Geometry Manipulation 英文高清PDF下载 阅读2021次,点赞0次
- 书籍翻译 – Fundamentals of Computer Graphics, Fourth Edition,第10章 Surface Shading中文翻译 阅读1355次,点赞3次
- 资源下载 - GPU Pro(1-7)英文原版高清PDF带书签下载 阅读11059次,点赞4次
- 深度学习 - 语音识别框架Wenet网络设计与实现 阅读243次,点赞0次
- C++ - sleep睡眠函数总结 阅读912次,点赞0次
- ThreeJS - 动态更换fbx模型的某个子Mesh现有的纹理贴图为指定的纹理贴图 阅读2540次,点赞1次
- 资源分享 - OpenGL 4.0 Shading Language Cookbook (First Edition) 英文高清PDF下载 阅读1581次,点赞0次
- TortoiseGit - 本地仓库更改远程仓库URL 阅读649次,点赞0次
评论
167