python - 这些代码有什么区别?为什么它们有不同的输出?
问题描述
list = ['first', 'second', 'third', 'fourth']
s = '/'
newList = s.join(list)
print(newList)
此代码输出
"first/second/third/fourth"
list = ['first', 'second', 'third', 'fourth']
s = '/'
newList = s.join(str(list))
print(newList)
此代码输出
"[/'/f/i/r/s/t/'/,/ /'/s/e/c/o/n/d/'/,/ /'/t/h/i/r/d/'/,/ /'/f/o/u/r/t/h/'/]"
这里做了什么str()
导致列表按每个字母分开?
解决方案
str()
创建一个字符串,如"['first', 'second', 'third', 'fourth']"
.
并将s.join()
字符串视为 char 数组。然后它放在'/'
数组中的每个元素之间。
推荐阅读
- swift - 嵌套“for”循环中并发SKAction序列的完成块?
- android - Android 8.0 adb 设备授权后一直未授权
- java - 创建字符串时执行加法
- ios - 关闭模态呈现的 VC 和弹出呈现的 VC
- asp.net - 通过 Jquery Asp.net Webforms 注册 RowCommand
- android - 找不到带有哈希字符串“android-P”的目标安装缺少的平台并同步项目
- ffmpeg - 从 Raspberry Pi 上的 USB 摄像头获取原始 h264 数据包
- google-cloud-platform - Google Cloud IOT,通过 API 阻止通信
- javascript - 如何识别纯文本网站地址并将它们包装在 href 标记中?
- excel - 使用范围粘贴到活动单元格