|I l@ve RuBoard|
3.10 Reversing a String by Words or Characters
Credit: Alex Martelli
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:
revwords = astring.split( ) # string -> list of words revwords.reverse( ) # reverse the list in place revwords = ' '.join(revwords) # list of strings -> string
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.
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:
def reverse(alist): temp = alist[:] temp.reverse( ) return temp
or maybe this, which is messier and slower:
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:
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.
3.10.4 See Also
The Library Reference section on sequence types; Perl Cookbook Recipe 1.6.
|I l@ve RuBoard|