Idiom for "last word in a string"
Scott David Daniels
Scott.Daniels at Acm.Org
Tue Sep 29 10:19:40 EDT 2009
Grant Edwards wrote:
> I recently ran across this construct for grabbing the last
> (whitespace delimited) word in a string:
> s.rsplit(None,1)[1]
> ... I've always done this:
> s.split()[-1]
> I was wondering what the advantage of the rsplit(None,1)[1]
> approach would be ...
Others have pointed out the efficiency reason (asking the machine
to do a pile of work that you intend to throw away). But nobody
warned you:
s.rsplit(None, 1)[-1]
would be better in the case of 'single_word'.rsplit(None, 1)
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list