⇤ ← Revision 1 as of 2004-09-11 16:05:24
Size: 2840
Comment:
|
Size: 4244
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 10: | Line 10: |
= 描述 = ... |
= 在字符串中倒转字符或单词(的顺序) = |
Line 17: | Line 15: |
你想要在一个字符串中倒转字符或单词(的顺序). | |
Line 21: | Line 20: |
字符串是不可变的,所以我们需要产生一个拷贝.一个列表正好可用作数据结构媒介,因为它有一个reverse方法,恰好适合做我们想要做的工作: | |
Line 29: | Line 29: |
对于翻转单词,我只是用一个单词列表取代上面的字符列表: |
|
Line 35: | Line 38: |
Note that we use a ' ' (space) joiner for the list of words, but a ' ' (empty string) joiner for the list of characters. | Note that we use a ' ' (space) joiner for the list of words, but a ' ' (empty string) joiner for the list of characters. 注意,我们使用一个' '(1个空格符)的join来连接单词列表,而用 ' '(空字符串)连接字符列表. |
Line 38: | Line 43: |
如果你需要在倒转词序时原样保留中间的空白字符,规则表达式会很有用的: |
|
Line 47: | Line 55: |
{{{ #!python }}} |
注意,这里再次使用了空字符串作为连接符,因为通过使用re.split并用一个包含括号分组的规则表达式作参数时,字串中的空白分隔符保留在了revwords列表中. |
Line 54: | Line 59: |
这个配方中的代码片断是快速的,可读的且Pythonic.可是,有些人总是说不清地迷信单行语句.如果你是这些人中的一个,你需要一个这样的附助函数(你可以从sitecustomize.py中把它们贴到你的"内置工具"(built-ins)中) |
|
Line 62: | Line 70: |
或也许是这样,这是杂乱且缓慢的: |
|
Line 69: | Line 80: |
这确实是单行的,但在我看来它没有价值. |
|
Line 70: | Line 83: |
无论如何,用如此这样的"内置工具"(almost-built-in),你现在能勇敢的写一个新的单行语句,如: |
|
Line 77: | Line 93: |
最终,python不会扭着你的胳膊让你去选择正确的途径:python给了你正确的工具,但使用它们是你的事. |
文章来自《Python cookbook》. 翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!
-- 0.706 [DateTime(2004-09-11T16:05:24Z)] TableOfContents
在字符串中倒转字符或单词(的顺序)
问题 Problem
You want to reverse the characters or words in a string.
你想要在一个字符串中倒转字符或单词(的顺序).
解决 Solution
Strings are immutable, so we need to make a copy. A list is the right intermediate data structure, since it has a reverse method that does just what we want and works in place:
字符串是不可变的,所以我们需要产生一个拷贝.一个列表正好可用作数据结构媒介,因为它有一个reverse方法,恰好适合做我们想要做的工作:
To flip words, we just work with a list of words instead of a list of characters:
对于翻转单词,我只是用一个单词列表取代上面的字符列表:
Note that we use a ' ' (space) joiner for the list of words, but a ' ' (empty string) joiner for the list of characters.
注意,我们使用一个' '(1个空格符)的join来连接单词列表,而用 ' '(空字符串)连接字符列表.
If you need to reverse by words while preserving untouched the intermediate whitespace, regular-expression splitting can be useful:
如果你需要在倒转词序时原样保留中间的空白字符,规则表达式会很有用的:
Note that the joiner becomes the empty string again in this case, because the whitespace separators are kept in the revwords list by using re.split with a regular expression that includes a parenthesized group.
注意,这里再次使用了空字符串作为连接符,因为通过使用re.split并用一个包含括号分组的规则表达式作参数时,字串中的空白分隔符保留在了revwords列表中.
讨论 Discussion
The snippets in this recipe are fast, readable, and Pythonic. However, some people have an inexplicable fetish for one-liners. If you are one of those people, you need an auxiliary function (you can stick it in your built-ins from sitecustomize.py) like this:
这个配方中的代码片断是快速的,可读的且Pythonic.可是,有些人总是说不清地迷信单行语句.如果你是这些人中的一个,你需要一个这样的附助函数(你可以从sitecustomize.py中把它们贴到你的"内置工具"(built-ins)中)
or maybe this, which is messier and slower:
或也许是这样,这是杂乱且缓慢的:
This is, indeed, in-lineable, but not worth it in my opinion.
这确实是单行的,但在我看来它没有价值.
Anyway, armed with such an almost-built-in, you can now do brave new one-liners, such as:
无论如何,用如此这样的"内置工具"(almost-built-in),你现在能勇敢的写一个新的单行语句,如:
In the end, Python does not twist your arm to make you choose the obviously right approach: Python gives you the right tools, but it's up to you to use them.
最终,python不会扭着你的胳膊让你去选择正确的途径:python给了你正确的工具,但使用它们是你的事.
参考 See Also
The Library Reference section on sequence types; Perl Cookbook Recipe 1.6.