4

I have a list of strings like this:

Item_has_was_updated_May_2010
Item_updated_Apr_2011
Item_got_updated_Sept_2011

I want to iterate through the list of string and update the last 2 parts of the string. The month and the year. The rest of the string I want to remain the same. The month and year will be taken from variables I have set earlier in my script, so let's call the month x and the year y.

My approach is to:

  1. Iterate through the list of strings
  2. Split each string by "_"
  3. Replace the last 2 items
  4. Join the items back together with the replaced items

The month and year will be taken from variables I have set earlier in my script, so let's call the month x and the year y.

If anyone can suggest a an approach, it is appreciated.

3
  • 1
    What are you asking for exactly? You've outlined a process that sounds reasonable, have you tried implementing it? Commented May 11, 2012 at 21:43
  • Lists in python an immutable, you can't change them.<br> However you can copy them to a new list and in doing that make changes. Commented May 11, 2012 at 21:44
  • 2
    Lists are mutable. Strings are immutable though. Commented May 11, 2012 at 21:44

3 Answers 3

9

You can do it without regular expressions by using str.rsplit:

yourlist = [s.rsplit('_', 2)[0] + '_' + x + '_' + y for s in yourlist]

See it working online: ideone


If you want to use formatting instead of string concatenation, try this:

yourlist = ['{}_{}_{}'.format(s.rsplit('_', 2)[0], x, y) for s in yourlist]

See it working online: ideone

Sign up to request clarification or add additional context in comments.

3 Comments

Could also do '_'.join(s.split('_')[:-2] + [x,y])
@NiklasB.: +1 Yeah that works too. It's slightly more concise. It does some unnecessary splitting and joining compared to using rsplit. Probably not a huge issue considering the size of the example strings though.
I think it might even be faster than the string concatenation (which needs to create a new string 4 times) The format approach is of course okay as well :)
2

I don't think you really need 're'.
You could use something like this:

m="dec"
y=2012
l=["Item_has_was_updated_May_2010",
    "Item_updated_Apr_2011",
    "Item_got_updated_Sept_2011"]
r=[]
for s in l:
    t=s.split("_")
    r.append("_".join(t[:-2])+"_%s_%s"%(m,y))

1 Comment

The r = []; for s in l: r.append(...) pattern is exactly what list comprehensions are good for :)
2
lst = [
   'Item_has_was_updated_May_2010',
   'Item_updated_Apr_2011',
   'Item_got_updated_Sept_2011',
]
month='Sep';year='2012'
for s in lst:
        list=s.split('_')
        list[-2:]=(month,year)
        r=''
        for a in list:
                r=r+"%s_"%a
        r=r[:-1]
        print r

Comments

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.