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

时间:2022-03-21 03:09:50

There is one thing that I do not understand...

有一件事我不明白......

Imagine you have a text = "hello world" and you want to split it.

想象一下,你有一个text =“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?

有什么不同?为什么你以某种方式或以其他方式做?你能给我一个理论解释吗?

5 个解决方案

#1


22  

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.split()(搜索split-functions)。

#2


13  

The string.split(stringobj) is a feature of the string module, which must be imported separately. Once upon a time, that was the only way to split a string. That's some old code you're looking at.

string.split(stringobj)是字符串模块的一个功能,必须单独导入。曾几何时,这是拆分字符串的唯一方法。那是你正在看的一些旧代码。

The stringobj.split() is a feature of a string object, stringobj, which is more recent than the string module. But pretty old, nonetheless. That's the current practice.

stringobj.split()是字符串对象stringobj的一个特性,它比字符串模块更新。但是很老了。这是目前的做法。

#3


5  

An extra note: str is the string type, as S.Lott points out above. That means that these two forms:

一个额外的注释:str是字符串类型,正如S.Lott指出的那样。这意味着这两种形式:

'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. In the second case, the string that gets passed in to str.split is used as self in the method.

...是等价的,因为str.split是未绑定的方法,而s.split是str对象的绑定方法。在第二种情况下,传入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.

有关绑定和未绑定方法的更多信息。

#4


5  

Short answer: the string module was the only way to perform these operations before python 1.6 - they've since been added to strings as methods.

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

#5


1  

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,则不足以产生巨大的差异。

#1


22  

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.split()(搜索split-functions)。

#2


13  

The string.split(stringobj) is a feature of the string module, which must be imported separately. Once upon a time, that was the only way to split a string. That's some old code you're looking at.

string.split(stringobj)是字符串模块的一个功能,必须单独导入。曾几何时,这是拆分字符串的唯一方法。那是你正在看的一些旧代码。

The stringobj.split() is a feature of a string object, stringobj, which is more recent than the string module. But pretty old, nonetheless. That's the current practice.

stringobj.split()是字符串对象stringobj的一个特性,它比字符串模块更新。但是很老了。这是目前的做法。

#3


5  

An extra note: str is the string type, as S.Lott points out above. That means that these two forms:

一个额外的注释:str是字符串类型,正如S.Lott指出的那样。这意味着这两种形式:

'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. In the second case, the string that gets passed in to str.split is used as self in the method.

...是等价的,因为str.split是未绑定的方法,而s.split是str对象的绑定方法。在第二种情况下,传入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.

有关绑定和未绑定方法的更多信息。

#4


5  

Short answer: the string module was the only way to perform these operations before python 1.6 - they've since been added to strings as methods.

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

#5


1  

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,则不足以产生巨大的差异。