##language:zh ''' 文章来自《Python cookbook》. 翻译仅仅是为了个人学习,其它商业版权纠纷与此无关! ''' -- 0.706 [<>] <> = 在字符串中倒转字符或单词(的顺序) = == 问题 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方法,恰好适合做我们想要做的工作: {{{ #!python revchars = list(astring) # string -> list of chars revchars.reverse( ) # reverse the list in place revchars = ''.join(revchars) # list of strings -> string }}} To flip words, we just work with a list of words instead of a list of characters: 对于翻转单词,我只是用一个单词列表取代上面的字符列表: {{{ #!python revwords = astring.split( ) # string -> list of words revwords.reverse( ) # reverse the list in place revwords = ' '.join(revwords) # list of strings -> string }}} 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: 如果你需要在倒转词序时原样保留中间的空白字符,规则表达式会很有用的: {{{ #!python import re revwords = re.split(r'(\s+)', astring) # separators too since '(...)' revwords.reverse( ) # reverse the list in place revwords = ''.join(revwords) # list of strings -> string }}} 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)中) {{{ #!python def reverse(alist): temp = alist[:] temp.reverse( ) return temp }}} or maybe this, which is messier and slower: 或也许是这样,这是杂乱且缓慢的: {{{ #!python def reverse_alternative(alist): return [alist[i-1] for i in range(len(alist), 0, -1)] }}} 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),你现在能勇敢的写一个新的单行语句,如: {{{ #!python revchars = ''.join(reverse(list(astring))) revwords = ' '.join(reverse(astring.split( ))) }}} 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.