1

I have a list of strings like

lst = ['foo000bar111', 'foo000bar1112', 'foo000bar1113']

and I want to extract the last numbers from each string to get

nums = ['111', '1112', '1113']

I have other numbers earlier in the string that I don't care about (000 in this example). There aren't spaces, so I can't lst.split() and I believe doing something like that without spacing is difficult. The numbers are of different lengths, so I can't just do str[-3:]. For what it's worth, the characters before the numbers I care about are the same in each string, and the numbers are at the end of the string.

I'm looking for a way to say 'ok, read until you find bar and then tell me what's the rest of the string.' The best I've come up with is [str[(str.index('bar')+3):] for str in lst], which works, but I doubt that's the most pythonic way to do it.

1
  • 1
    You are already doing it correct. Commented Jun 11, 2015 at 20:44

3 Answers 3

5

Your method is accurate. You can also try using re

>>> import re
>>> lst = ['foo000bar111', 'foo000bar1112', 'foo000bar1113']
>>> [re.search(r'(\d+$)',i).group() for i in lst]
['111', '1112', '1113']

You can also try rindex

>>> [i[i.rindex('r')+1:] for i in lst]
['111', '1112', '1113']
Sign up to request clarification or add additional context in comments.

Comments

2

Your solution is not bad at all, but you could improve it in a couple of ways:

  1. Use rindex() instead of index; if bar should happen to occur twice (or more) in a string, you want to find the last instance.

  2. Or you can use rsplit():

    [ s.rsplit("bar", 1)[1] for s in lst ]
    

Edit: @Bas beat me to the second solution by a few seconds! :-)

Comments

2

Your own solution works well enough, but I think the main problem with is that you have to hard-code the length of the search string you are using. This could be solved using a temporary variable like this:

tag = 'bar'
[s[(s.index(tag)+len(tag)):] for s in lst]

One alternative way using rsplit:

[x.rsplit('bar', 1)[1] for x in lst]

This always splits on the last occurrence of bar, even if it occurs more than once.

3 Comments

Thanks for the suggestions. I actually had to do a little extra because I needed to remove a file extension, and rsplit worked great.
This is really great idea and you get my vote for it. However please change the variable from str to something else in the first list comp as str is a built in variable name. :)
@BhargavRao Thanks, fixed. I copied that code from the question without looking too much, I try not to do that in my own code ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.