2.5(分区)中的新字符串方法 [英] new string method in 2.5 (partition)

查看:84
本文介绍了2.5(分区)中的新字符串方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我的兴奋,特别是如果你已经意识到这一点,但

这似乎是一种容易被忽视的功能(但是可以

非常有用):

8位和Unicode字符串都有新的分区(sep)和

rpartition(sep)方法,简化了常见的用例。

find(S)方法通常用于获取索引,然后用于
切割字符串并获取

分隔符之前和之后的碎片。 partition(sep)将此模式压缩为单个方法

调用,返回包含

分隔符之前的子字符串的3元组,分隔符本身以及之后的子字符串分隔符。

如果找不到分隔符,则元组的第一个元素是

整个字符串,其他两个元素为空。 rpartition(sep)也

返回一个3元组,但是从字符串的末尾开始搜索;

" r"代表''反向''。


一些例子:


>>('''http://www.python.org'')。partition(''://'')



(''http'',''://'','''www.python.org'')


>>(''file:/usr/share/doc/index.html'')。partition('':// '')



(''file:/usr/share/doc/index.html'','''', '''')


>>(你们的主题:一个简单的问题'')。partition('':'')



(你的主题'',你':''' ,你''一个简单的问题'')


>>''www.python.org' '.rpartition(''。'')



(''''www.python'',''。''',' 'org'')


>>'''www.python.org'' .rpartition('':'')



('''''''''''''www.python。 org'')


(由Fredrik Lundh根据Raymond Hettinger的建议实施。)

解决方案

谢谢你的提问。


John Salerno写道:


原谅我的兴奋,特别是如果你已经知道的话这个,但是

这看起来像是我的那种功能很容易被忽视(但是可以

非常有用):


8位和Unicode字符串都有新的分区(sep)和

rpartition(sep)简化常见用例的方法。

find(S)方法通常用于获取索引,然后用于

切片字符串并获取

分隔符之前和之后的片段。 partition(sep)将此模式压缩为单个方法

调用,返回包含

分隔符之前的子字符串的3元组,分隔符本身以及之后的子字符串分隔符。

如果找不到分隔符,则元组的第一个元素是

整个字符串,其他两个元素为空。 rpartition(sep)也

返回一个3元组,但是从字符串的末尾开始搜索;

" r"代表''反向''。


一些例子:


>>('''http://www.python.org'')。partition(''://'')



(''http'',''://'','''www.python.org'')


>>(''file:/usr/share/doc/index.html'')。partition(''://'')



(''file:/usr/share/doc/index.html'','''','''')


>>(你们的主题:一个简单的问题'')。分区('':'')



(你的''主题'',你':'','你是一个快速的问题'')


> > '' www.python.org .rpartition(''。'')



('''www.python'',''。''',''org'')


>>''www.python.org''.rpartition('':'')



('''','''''''''www.python.org'')


(由Fredrik Lundh根据Raymond Hettinger的建议实施。)


我很困惑。

什么是这与string.split有什么区别?

John Salerno写道:


原谅我的兴奋,特别是如果你已经知道的话这个,但是

这似乎很容易被忽视的功能(但可能会非常有用):


8位和Unicode字符串都有新的分区(sep)和

rpartition(sep)方法,简化了常见用例。

find(S)方法通常用于获取索引,然后用于
切割字符串并获取之前和之后的部分

分隔符。 partition(sep)将此模式压缩为单个方法

调用,返回包含

分隔符之前的子字符串的3元组,分隔符本身以及之后的子字符串分隔符。

如果找不到分隔符,则元组的第一个元素是

整个字符串,其他两个元素为空。 rpartition(sep)也

返回一个3元组,但是从字符串的末尾开始搜索;

" r"代表''反向''。


一些例子:


>>('''http://www.python.org'')。partition(''://'')



(''http'',''://'','''www.python.org'')


>>(''file:/usr/share/doc/index.html'')。partition(''://'')



(''file:/usr/share/doc/index.html'','''','''')


>>(你们的主题:一个简单的问题'')。分区('':'')



(你的''主题'',你':'','你是一个快速的问题'')


> > '' www.python.org .rpartition(''。'')



('''www.python'',''。''',''org'')


>>''www.python.org''.rpartition('':'')



('''','''''''''www.python.org'')


(由Fredrik Lundh根据Raymond Hettinger的建议实施。)


ri ************ @ gmail.com < ri ************ @ gmail.comwrote:


这与string.split的区别是什么?


>>(''http://www.python .org'')。partition(''://'')



(''http'','': //'','''www.python.org'')


>> ;('''http://www.python.org'')。split(''://'')



[''http'','''www.python.org'']


-

劳伦斯 - http://www.oluyede.org/blog

没有比这更危险的了一个想法

如果它是你唯一拥有的那个 - E. A. Chartier


Forgive my excitement, especially if you are already aware of this, but
this seems like the kind of feature that is easily overlooked (yet could
be very useful):
Both 8-bit and Unicode strings have new partition(sep) and
rpartition(sep) methods that simplify a common use case.
The find(S) method is often used to get an index which is then used to
slice the string and obtain the pieces that are before and after the
separator. partition(sep) condenses this pattern into a single method
call that returns a 3-tuple containing the substring before the
separator, the separator itself, and the substring after the separator.
If the separator isn''t found, the first element of the tuple is the
entire string and the other two elements are empty. rpartition(sep) also
returns a 3-tuple but starts searching from the end of the string; the
"r" stands for ''reverse''.

Some examples:

>>(''http://www.python.org'').partition(''://'')

(''http'', ''://'', ''www.python.org'')

>>(''file:/usr/share/doc/index.html'').partition(''://'')

(''file:/usr/share/doc/index.html'', '''', '''')

>>(u''Subject: a quick question'').partition('':'')

(u''Subject'', u'':'', u'' a quick question'')

>>''www.python.org''.rpartition(''.'')

(''www.python'', ''.'', ''org'')

>>''www.python.org''.rpartition('':'')

('''', '''', ''www.python.org'')

(Implemented by Fredrik Lundh following a suggestion by Raymond Hettinger.)

解决方案

sweet thanks for the heads up.

John Salerno wrote:

Forgive my excitement, especially if you are already aware of this, but
this seems like the kind of feature that is easily overlooked (yet could
be very useful):
Both 8-bit and Unicode strings have new partition(sep) and
rpartition(sep) methods that simplify a common use case.
The find(S) method is often used to get an index which is then used to
slice the string and obtain the pieces that are before and after the
separator. partition(sep) condenses this pattern into a single method
call that returns a 3-tuple containing the substring before the
separator, the separator itself, and the substring after the separator.
If the separator isn''t found, the first element of the tuple is the
entire string and the other two elements are empty. rpartition(sep) also
returns a 3-tuple but starts searching from the end of the string; the
"r" stands for ''reverse''.

Some examples:

>>(''http://www.python.org'').partition(''://'')

(''http'', ''://'', ''www.python.org'')

>>(''file:/usr/share/doc/index.html'').partition(''://'')

(''file:/usr/share/doc/index.html'', '''', '''')

>>(u''Subject: a quick question'').partition('':'')

(u''Subject'', u'':'', u'' a quick question'')

>>''www.python.org''.rpartition(''.'')

(''www.python'', ''.'', ''org'')

>>''www.python.org''.rpartition('':'')

('''', '''', ''www.python.org'')

(Implemented by Fredrik Lundh following a suggestion by Raymond Hettinger.)


I''m confused.
What''s the difference between this and string.split?

John Salerno wrote:

Forgive my excitement, especially if you are already aware of this, but
this seems like the kind of feature that is easily overlooked (yet could
be very useful):
Both 8-bit and Unicode strings have new partition(sep) and
rpartition(sep) methods that simplify a common use case.
The find(S) method is often used to get an index which is then used to
slice the string and obtain the pieces that are before and after the
separator. partition(sep) condenses this pattern into a single method
call that returns a 3-tuple containing the substring before the
separator, the separator itself, and the substring after the separator.
If the separator isn''t found, the first element of the tuple is the
entire string and the other two elements are empty. rpartition(sep) also
returns a 3-tuple but starts searching from the end of the string; the
"r" stands for ''reverse''.

Some examples:

>>(''http://www.python.org'').partition(''://'')

(''http'', ''://'', ''www.python.org'')

>>(''file:/usr/share/doc/index.html'').partition(''://'')

(''file:/usr/share/doc/index.html'', '''', '''')

>>(u''Subject: a quick question'').partition('':'')

(u''Subject'', u'':'', u'' a quick question'')

>>''www.python.org''.rpartition(''.'')

(''www.python'', ''.'', ''org'')

>>''www.python.org''.rpartition('':'')

('''', '''', ''www.python.org'')

(Implemented by Fredrik Lundh following a suggestion by Raymond Hettinger.)


ri************@gmail.com <ri************@gmail.comwrote:

What''s the difference between this and string.split?

>>(''http://www.python.org'').partition(''://'')

(''http'', ''://'', ''www.python.org'')

>>(''http://www.python.org'').split(''://'')

[''http'', ''www.python.org'']

--
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it''s the only one you have" - E. A. Chartier


这篇关于2.5(分区)中的新字符串方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆