-2

I need to replace the string in last occurrence after splitting the string

I have tried the below way but it is giving incorrect output like 1.120

below is the code which I have tried.

y = "1.19-test"


if '-' in y:
    splt = (int(y.split('-')[0][-1]) + 1)
    str = y[::-1].replace(y.split('-')[0][-1], str(splt)[::-1], 1)[::-1]
    print str
else:
    splt = (int(y.split('.')[-1]) + 1)
    str = y[::-1].replace(y.split('-')[0][-1], str(splt)[::-1], 1)[::-1]
    print str

The output I'm getting like 1.120-test. But here I need the output as 1.20-test

moong
  • 73
  • 1
  • 1
  • 9
  • 2
    Perhaps you could post some more examples of input and desired output, to give a clearer idea of what you are trying to do. – khelwood Feb 13 '19 at 11:37

3 Answers3

0

As per my understanding you need something like this:

y = "1-19"

str1 = ''
if '-' in y:
    splt = y.split('-')
    str1 = "%s-%s"%(splt[0], int(splt[-1])+1)
else:
    splt = y.split('.')
    str1 = "%s.%s"%(splt[0], int(splt[-1])+1)
print str1
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
  • It is not working if we have y = ```"1.19-test"```. Giving error ```ValueError: invalid literal for int() with base 10: 'test'```. The output I need is ```1.20-test``` – moong Feb 13 '19 at 12:54
  • Then modify above code to handle - and it will work – Amit Nanaware Feb 13 '19 at 13:36
0

Too complicated. Just store the output of the split, make your changes, and use .join method to get back the desired string. Edit Based on the updated question, you also need to handle some extra characters beforehand. Assuming you only want to increment the portion after a . you can just keep track of the extra characters in a leftover variable, before applying the splitting logic.

y = "1.19-test"
leftover = ''
if '-' in y:
    temp_y, leftover = y[:y.index('-')], y[y.index('-'):]
else:
    temp_y = y

split_list = temp_y.split('.')
split_list[-1] = str(int(split_list[-1]) + 1) #convert last value to int, add 1, convert result back to string.
result = '.'.join(split_list) #joins all items in the list using "."
result += leftover #add back any leftovers
print(result)
#Output:
1.20-test
Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
  • It is not working if we have ```y = "1.19-test"```. Giving error ```ValueError: invalid literal for int() with base 10: 'test'```. The output I need is ```1.20-test``` – moong Feb 13 '19 at 12:52
  • I wouldn't have expected it to work with an input like that anyways. You may want to edit your question or ask a new one. " need to replace the string in last occurrence after splitting the string" Well, clearly thats not the last occurance with an input like that. This input should be added in question. – Paritosh Singh Feb 13 '19 at 12:56
  • Updated the Question.. Please help me on this – moong Feb 13 '19 at 12:58
  • @moong Alright, updated my answer. – Paritosh Singh Feb 13 '19 at 13:09
  • It worked... Thanks @Parithosh Singh – moong Feb 13 '19 at 13:18
0

The below code worked, I took reference of @Paritosh Singh code.

y = "1.19"
if '-' in y:
    temp = y.split('-')[0]
    splitter = '.'
    split_list = temp.split(splitter)
    split_list[-1] = str(int(split_list[-1]) + 1)
    result = splitter.join(split_list)
    print(result)
    print result+'-'+y.split('-')[1]

else:
    splitter = '.'
    split_list = y.split(splitter)
    split_list[-1] = str(int(split_list[-1]) + 1)
    result = splitter.join(split_list)
    print(result)
moong
  • 73
  • 1
  • 1
  • 9