首页 > python中的split函数中的参数问题

python中的split函数中的参数问题

str = 'server=mpilgrim'
print str.split('=', 1)[0]
print str.split('=', 1)[1]
print str.split('=')[0]
print str.split('=')[1]

split函数中的第二个参数到底有什么意义,我发现上述两个结果是一样的,文档中说是控制行,但是我真的没发现有什么用处阿,求大神解释一下。


首先吐槽下不知道为什么要给这个问题打负分。
根据问题,第二个参数是给出最多可进行的分割次数。如果最多分割次数不给出则会按照最多的匹配次数进行分割。


没看到什么控制行的,或者你说的是涉及到标签中几个框架的代码!

 |  split(...)
 |      S.split([sep [,maxsplit]]) -> list of strings
 |      
 |      Return a list of the words in the string S, using sep as the
 |      delimiter string.  If maxsplit is given, at most maxsplit
 |      splits are done. If sep is not specified or is None, any
 |      whitespace string is a separator and empty strings are removed
 |      from the result.

第二个参数表明是split次数。 用下面这个例子测试:

str = 'server=mpilgrim&ip=10.10.10.10&port=8080'
print str.split('=', 1)[0]
print str.split('=', 1)[1]
print str.split('=')[0]
print str.split('=')[1]

结果是:

server
mpilgrim&ip=10.10.10.10&port=8080
server
mpilgrim&ip

【热门文章】
【热门文章】