为什么 Python "".split() 和 "".split(",") 会产生不同的结果?
Why do Python "".split() and "".split(",") produce a different result?
这是我在 Python.
中将 split() 应用于具有默认分隔符和“,”作为分隔符的空字符串时的结果
>>> print "".split(',')
['']
>>> print "".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.
仅当您不指定分隔符时才会删除空字符串。
使用 help
来自 Python 的交互式提示
$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = ""
>>> s.split()
[]
>>> help(s.split)
这提供了上面引用的信息。
这是我在 Python.
中将 split() 应用于具有默认分隔符和“,”作为分隔符的空字符串时的结果>>> print "".split(',')
['']
>>> print "".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.
仅当您不指定分隔符时才会删除空字符串。
使用 help
来自 Python 的交互式提示
$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = ""
>>> s.split()
[]
>>> help(s.split)
这提供了上面引用的信息。