Jam*_*nco -3 python string iterable
我熟悉这个split功能,我会这样使用它:
str = "Line1-abcdef \nLine2-abc \nLine4-abcd"
print str.split( )
Run Code Online (Sandbox Code Playgroud)
以上将返回:
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
Run Code Online (Sandbox Code Playgroud)
简单易行.但是,我遇到了一段包含此声明的代码:
(line, str) = str.split("\n", 1)
Run Code Online (Sandbox Code Playgroud)
这里有两件事我不明白:
第二个参数split和它的作用.我看了看这里,它说的是行数.那是什么意思?
split返回一个可迭代的向量.为什么要分配(line, str)?(line, str)这里的意思是什么?
第二个参数maxsplit=1表示在遇到分隔符\n一次后停止分割.
因此,您只有两个部分,即您的行和字符串的其余部分.
例如:
str = 'This is one line\nThis is a second line\nThis is a third line'
(line, str) = str.split('\n', 1)
print(line)
# 'This is one line'
print(str)
# 'This is a second line\nThis is a third line'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
850 次 |
| 最近记录: |