logo

Python – pakeiskite visus eilutės poeilutės atvejus

Kartais dirbdami su Python eilutėmis galime susidurti su problema, kai turime pakeisti visus poeilutės atvejus kitais.

Įvestis: test_str = geeksforgeeks s1 = geeks s2 = abcd
Išvestis: test_str = abcdforabcd Paaiškinimas: Visus s1 atvejus pakeičiame s2 test_str.



Įvestis: test_str = geeksforgeeks s1 = s2 = abcd
Išvestis: test_str = geeksabcdgeeks

1 metodas

Galime naudoti įmontuotą python3 funkciją pakeisti, kad pakeistume visus poeilutės atvejus.



Diegimas naudojant integruotą funkciją: -

Python3






#Python has inbuilt function replace to replace all occurrences of substring.> input_string>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> input_string>=> input_string.replace(s1, s2)> print>(input_string)>

>

>

Išvestis

ekta kapoor aktorius
abcdforabcd>

Laiko sudėtingumas: O(n)
Pagalbinė erdvė: O(n)

2 metodas:

Naudojama eilutės padalijimas po eilutę ir pakeitimas nauja string.split() funkcija.

Python3




#code for replacing all occurrences of substring s1 with new string s2> test_str>=>'geeksforgeeks'> s1>=>'geeks'> s2>=>'abcd'> #string split by substring> s>=>test_str.split(s1)> new_str>=>''> for> i>in> s:> >if>(i>=>=>''):> >new_str>+>=>s2> >else>:> >new_str>+>=>i> #printing the replaced string> print>(new_str)> #contributed by Bhavya Koganti>

>

>

Išvestis

abcdforabcd>

Visų metodų laiko ir erdvės sudėtingumas yra vienodas:

Laiko sudėtingumas: O(n)

Pagalbinė erdvė: O(n)

3 būdas: Kitas būdas pakeisti visus eilutę eilutėje yra naudoti re.sub() funkcija iš re modulio python.

Python3


Šarvanandas



import> re> def> replace_substring(test_str, s1, s2):> ># Replacing all occurrences of substring s1 with s2> >test_str>=> re.sub(s1, s2, test_str)> >return> test_str> # test> test_str>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> print>(replace_substring(test_str, s1, s2))>

>

>

Išvestis

abcdforabcd>

Laiko sudėtingumas: O(n), kur n yra įvesties eilutės ilgis. Taip yra todėl, kad funkcija re.sub() kartojasi per visą įvesties eilutę ir atlieka reguliariosios išraiškos atitiktį kiekvienam simboliui, kad surastų visus poeilutės atvejus. Iteracijų skaičius yra tiesiogiai proporcingas įvesties eilutės ilgiui.
Pagalbinė erdvė: nauja

4 būdas: paprastos iteracijos naudojimas

Šio metodo idėja yra kartoti įvesties eilutę po simbolio ir patikrinti, ar kiekviena m ilgio poeilutė atitinka eilutę, kurią norime pakeisti. Jei taip, prie rezultato pridedame pakaitinę eilutę ir perkeliame žymeklį į priekį m simbolių. Jei nesutampa, prie rezultato pridedame esamą simbolį ir perkeliame žymeklį 1 simboliu į priekį.

Python3




def> replace_substring(test_str, s1, s2):> ># Initialize an empty string to store the result> >result>=> ''> ># Initialize a variable to keep track of our position in the string> >i>=> 0> ># Loop through the string one character at a time> >while> i <>len>(test_str):> ># Check if the current substring matches the substring we want to replace> >if> test_str[i:i>+>len>(s1)]>=>=> s1:> ># If it does, add the replacement substring to the result and move the pointer forward> >result>+>=> s2> >i>+>=> len>(s1)> >else>:> ># If it doesn't, add the current character to the result and move the pointer forward> >result>+>=> test_str[i]> >i>+>=> 1> ># Return the final result> >return> result> # test> test_str>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> print>(replace_substring(test_str, s1, s2))>

>

>

Išvestis

abcdforabcd>

Laiko sudėtingumas: O (nm), kur n yra įvesties eilutės ilgis, o m yra pakeičiamos eilutės ilgis.
Pagalbinė erdvė: O(n), nes kuriame naują eilutę rezultatui išsaugoti.