Idiom for "last word in a string"

G

Grant Edwards

I recently ran across this construct for grabbing the last
(whitespace delimited) word in a string:

s.rsplit(None,1)[1]

It was somewhat obvious from the context what it was supposed
to do, but it took a bit of Googling to figure out exactly what
was going on.

When I want the last word in a string, I've always done this:

s.split()[-1]

I was wondering what the advantage of the rsplit(None,1)[1]
approach would be other than inducing people to learn about the
maxsplit argument that is accepted by the split() methods?
 
A

akonsu

I recently ran across this construct for grabbing the last
(whitespace delimited) word in a string:

   s.rsplit(None,1)[1]

It was somewhat obvious from the context what it was supposed
to do, but it took a bit of Googling to figure out exactly what
was going on.

When I want the last word in a string, I've always done this:

   s.split()[-1]

I was wondering what the advantage of the rsplit(None,1)[1]
approach would be other than inducing people to learn about the
maxsplit argument that is accepted by the split() methods?

hello,
perhaps rsplit generates as many elements in the list as absolutely
necesary compared to the whole list returned by split()?
konstantin
 
C

Christos Trochalakis

At Wed, 23 Sep 2009 18:47:05 +0000 (UTC),
Grant said:
I recently ran across this construct for grabbing the last
(whitespace delimited) word in a string:

s.rsplit(None,1)[1]

It was somewhat obvious from the context what it was supposed
to do, but it took a bit of Googling to figure out exactly what
was going on.

When I want the last word in a string, I've always done this:

s.split()[-1]

I was wondering what the advantage of the rsplit(None,1)[1]
approach would be other than inducing people to learn about the
maxsplit argument that is accepted by the split() methods?

s.rsplit(None, 1) is a cheaper operation because it splits the string
*only once*.
 
C

Christos Trochalakis

At Wed, 23 Sep 2009 18:47:05 +0000 (UTC),
Grant said:
I recently ran across this construct for grabbing the last
(whitespace delimited) word in a string:

s.rsplit(None,1)[1]

It was somewhat obvious from the context what it was supposed
to do, but it took a bit of Googling to figure out exactly what
was going on.

When I want the last word in a string, I've always done this:

s.split()[-1]

I was wondering what the advantage of the rsplit(None,1)[1]
approach would be other than inducing people to learn about the
maxsplit argument that is accepted by the split() methods?

s.rsplit(None, 1) is a cheaper operation because it splits the string
*only once*.
 
C

Christos Trochalakis

At Wed, 23 Sep 2009 18:47:05 +0000 (UTC),
Grant said:
I recently ran across this construct for grabbing the last
(whitespace delimited) word in a string:

s.rsplit(None,1)[1]

It was somewhat obvious from the context what it was supposed
to do, but it took a bit of Googling to figure out exactly what
was going on.

When I want the last word in a string, I've always done this:

s.split()[-1]

I was wondering what the advantage of the rsplit(None,1)[1]
approach would be other than inducing people to learn about the
maxsplit argument that is accepted by the split() methods?

s.rsplit(None, 1) is a cheaper operation because it splits the string
*only once*.
 
C

Christos Trochalakis

At Wed, 23 Sep 2009 18:47:05 +0000 (UTC),
Grant said:
I recently ran across this construct for grabbing the last
(whitespace delimited) word in a string:

s.rsplit(None,1)[1]

It was somewhat obvious from the context what it was supposed
to do, but it took a bit of Googling to figure out exactly what
was going on.

When I want the last word in a string, I've always done this:

s.split()[-1]

I was wondering what the advantage of the rsplit(None,1)[1]
approach would be other than inducing people to learn about the
maxsplit argument that is accepted by the split() methods?

s.rsplit(None, 1) is a cheaper operation because it splits the string
*only once*.
 
P

Peter Otten

akonsu said:
I recently ran across this construct for grabbing the last
(whitespace delimited) word in a string:

s.rsplit(None,1)[1]

It was somewhat obvious from the context what it was supposed
to do, but it took a bit of Googling to figure out exactly what
was going on.

When I want the last word in a string, I've always done this:

s.split()[-1]

I was wondering what the advantage of the rsplit(None,1)[1]
approach would be other than inducing people to learn about the
maxsplit argument that is accepted by the split() methods?

hello,
perhaps rsplit generates as many elements in the list as absolutely
necesary compared to the whole list returned by split()?
konstantin

Indeed, and if the string is long it has a measurable effect:

$ python -m timeit -s"s = 'oneword '*1000" "s.rsplit(None, 1)[-1]"
100000 loops, best of 3: 2.23 usec per loop
$ python -m timeit -s"s = 'oneword '*1000" "s.split()[-1]"
1000 loops, best of 3: 191 usec per loop

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top