简体   繁体   English

string.split(text)或text.split():有什么区别?

[英]string.split(text) or text.split() : what's the difference?

There is one thing that I do not understand... 我不明白一件事...

Imagine you have a text = "hello world" and you want to split it. 假设您有一个文本 =“ hello world”,并且想要拆分它。

In some places I see people that want to split the text doing: 在某些地方,我看到有人想要拆分文本

string.split(text)

In other places I see people just doing: 在其他地方,我看到人们只是在做:

text.split()

What's the difference? 有什么不同? Why you do in one way or in the other way? 为什么以一种方式或另一种方式进行操作? Can you give me a theory explanation about that? 你能给我一个理论上的解释吗?

Interestingly, the docstrings for the two are not completely the same in Python 2.5.1: 有趣的是,两者的文档字符串在Python 2.5.1中并不完全相同:

>>> import string
>>> help(string.split)
Help on function split in module string:

split(s, sep=None, maxsplit=-1)
    split(s [,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, splits at no more than
    maxsplit places (resulting in at most maxsplit+1 words).  If sep
    is not specified or is None, any whitespace string is a separator.

    (split and splitfields are synonymous)

>>> help("".split)
Help on built-in function 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.

Digging deeper, you'll see that the two forms are completely equivalent, as string.split(s) actually calls s.split() (search for the split -functions). 深入研究,您会发现这两种形式是完全等效的,因为string.split(s)实际上调用s.split() (搜索split- functions)。

The string.split(stringobj) is a feature of the string module, which must be imported separately. string.split(stringobj)string模块的功能,必须单独导入。 Once upon a time, that was the only way to split a string. 曾几何时,这是拆分字符串的唯一方法。 That's some old code you're looking at. 那是您正在查看的一些旧代码。

The stringobj.split() is a feature of a string object, stringobj , which is more recent than the string module. stringobj.split()是字符串对象stringobj ,它比string模块更新。 But pretty old, nonetheless. 但是,尽管很老。 That's the current practice. 这是当前的做法。

An extra note: str is the string type, as S.Lott points out above. 额外说明: str是字符串类型,正如S.Lott在上面指出的那样。 That means that these two forms: 这意味着这两种形式:

'a b c'.split()
str.split('a b c')

# both return ['a', 'b', 'c']

...are equivalent, because str.split is the unbound method, while s.split is a bound method of a str object. ...是等效的,因为str.split是未绑定方法,而s.splitstr对象的绑定方法。 In the second case, the string that gets passed in to str.split is used as self in the method. 在第二种情况下,传递给str.split的字符串在该方法中用作self

This doesn't make much difference here, but it's an important feature of how Python's object system works. 这在这里并没有太大的区别,但这是Python对象系统如何工作的重要特征。

More info about bound and unbound methods. 有关绑定和未绑定方法的更多信息。

简短的答案:在python 1.6之前,字符串模块是执行这些操作的唯一方法-此后它们已作为方法添加到字符串中。

Use whichever you like, but realize that str.split is the recommended way of doing it. 使用任何您喜欢的方法,但要意识到str.split是推荐的方法。 :-) :-)

string.split is a tad older method of doing the same thing. string.split是做同样事情的一种较旧的方法。

str.split is a bit more efficient (since you don't have to import the string module or look up any names from it), but not enough to make a huge difference if you prefer string.split. str.split效率更高(因为您不必导入字符串模块或从中查找任何名称),但是如果您喜欢string.split,则不足以产生巨大的变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM