2016-06-09 7 views
1

Ich habe eine Zeichenfolge und ich möchte the jedes Mal nach seinem zweiten Auftreten ersetzen.String ersetzen, ohne die erste Instanz zu ersetzen

s = "change the string in the sentence and the save" 

würde Ich mag die das Wort the zu hello ersetzen. Aber außer dem ersten.

Ausgang sollte sein:

change the string in hello sentence and hello save 

Antwort

1

Try this:

def replace_not_first(str, word, replace): 
    str_arr = str.split(word) 
    return str_arr[0] + word + replace.join(str_arr[1:]) 

str = "change the string in the sentence and the save" 
print(replace_not_first(str, 'the', 'hello')) 

druckt: change the string in hello sentence and hello save

1

Diese

string = "change the string in the sentence and the save" 
the_arr = string.split("the") 
print the_arr[0] + "the" + "hello".join(the_arr[1:])` 
+0

Ich möchte den ersten Wert nicht ändern. Ich muss von der zweiten selbst –

+3

ändern Dies funktioniert nicht, wenn '** Hallo **' ist bereits in der Zeichenfolge und befindet sich vor dem ersten 'the'. Wie zum Beispiel: '** Hallo ** ändern Sie die Zeichenfolge im Satz und das Speichern ' –

+0

@lamitumparkar netten kleinen Trick. – bhansa

1

Versuchen Sie folgendes Motto s arbeiten sollten Lösung.

string = 'change the string in the sentence and the save' 
new_string = string[:string.find('the')+3] + string[string.find('the')+3:].replace('the', 'hello') 
1

Ich hoffe, dass dies den Job machen wird.

str = str.partition('the')[0] + str.partition('the')[1] + str.partition('the')[-1].replace('the','hello') 
+0

'str.partition ('the')' muss den gesamten Text durchlaufen und Ihr Code verschwendet es verschwenderisch dreimal. –

+0

Do: 'zuerst, die, Rest = str.partition ('the'); str = erster + der + rest.replace ('der', 'hallo') ' – Bakuriu

1
>>> str = "change the string in the sentence and the save" 
>>> str.replace('the', 'hello') 
>>> str.replace('hello', 'the',1) 
+0

Ich denke, das ist nicht der richtige Weg. coz was ist, wenn der Satz ist wie "Hallo, ändern Sie die Zeichenfolge im Satz und das Speichern". Ihr Code wird das erste Hallo ersetzen, das in der ursprünglichen Zeichenkette ist. – Harsha

3

würde ich den String von rechts mit dem Wort aufspalten Sie verwenden str.rsplit() Funktion ersetzen, gehen, aber aufgeteilt nur s.count('the') - 1 mal.

Dann kommen die Ausgabeliste mit hello:

>>> s.rsplit('the', s.count('the') - 1) 
['change the string in ', ' sentence and ', ' save'] 

>>> 'hello'.join(s.rsplit('the', s.count('the') - 1)) 
'change the string in hello sentence and hello save' 
0

Sie die Zeichenfolge in Teile aufspalten:

string = "change the string in the sentence and the save" 

splitString = string.split() 
firstIndex = splitString.index('the') 

part1 = ' '.join(splitString[:firstIndex+1]) 
part2 = ' '.join(splitString[firstIndex+1:]).replace('the','hello') 

newString = part1 + ' ' + part2 

Oder in einer Zeile:

newString = ' '.join(['hello' if j == 'the' and i != string.split().index('the') else j for i, j in enumerate(string.split())]) 
1

Try this:

>>> s = "change the string in the sentence and the save" 
>>> s.split("the",1)[0]+"the" + s.split("the",1)[1].replace("the","hello") 
'change the string in hello sentence and hello save'